CUBRID Engine
type_checking.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008 Search Solution Corporation. All rights reserved by Search Solution.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  */
19 
20 /*
21  * type_checking.c - auxiliary functions for parse tree translation
22  */
23 
24 #ident "$Id$"
25 
26 #include "config.h"
27 
28 #include <assert.h>
29 #include <stdarg.h>
30 #include <ctype.h>
31 #include <float.h>
32 #include <math.h>
33 #include <limits.h>
34 
35 #if defined(WINDOWS)
36 #include "porting.h"
37 #include "wintcp.h"
38 #else /* ! WINDOWS */
39 #include <sys/time.h>
40 #endif /* ! WINDOWS */
41 
42 #include "error_manager.h"
43 #include "parser.h"
44 #include "parser_message.h"
45 #include "set_object.h"
46 #include "arithmetic.h"
47 #include "string_opfunc.h"
48 #include "object_domain.h"
49 #include "semantic_check.h"
50 #include "xasl_generation.h"
51 #include "language_support.h"
52 #include "schema_manager.h"
53 #include "system_parameter.h"
54 #include "network_interface_cl.h"
55 #include "object_template.h"
56 #include "db.h"
57 
58 #include "dbtype.h"
59 
60 #define SET_EXPECTED_DOMAIN(node, dom) \
61  do \
62  { \
63  (node)->expected_domain = (dom); \
64  if ((node)->or_next) \
65  { \
66  PT_NODE *_or_next = (node)->or_next; \
67  while (_or_next) \
68  { \
69  if (_or_next->type_enum == PT_TYPE_MAYBE && _or_next->expected_domain == NULL) \
70  { \
71  _or_next->expected_domain = (dom); \
72  } \
73  _or_next = _or_next->or_next; \
74  } \
75  } \
76  } \
77  while (0)
78 
79 #if defined(ENABLE_UNUSED_FUNCTION)
80 typedef struct generic_function_record
81 {
82  const char *function_name;
83  const char *return_type;
84  int func_ptr_offset;
85 } GENERIC_FUNCTION_RECORD;
86 
87 static int pt_Generic_functions_sorted = 0;
88 
89 static GENERIC_FUNCTION_RECORD pt_Generic_functions[] = {
90  /* Make sure that this table is in synch with the generic function table. Don't remove the first dummy position.
91  * It's a place holder. */
92  {"AAA_DUMMY", "integer", 0}
93 };
94 #endif /* ENABLE_UNUSED_FUNCTION */
95 
96 #define PT_ARE_COMPARABLE_CHAR_TYPE(typ1, typ2) \
97  ((PT_IS_SIMPLE_CHAR_STRING_TYPE (typ1) && PT_IS_SIMPLE_CHAR_STRING_TYPE (typ2)) \
98  || (PT_IS_NATIONAL_CHAR_STRING_TYPE (typ1) && PT_IS_NATIONAL_CHAR_STRING_TYPE (typ2)))
99 
100 #define PT_ARE_COMPARABLE_NUMERIC_TYPE(typ1, typ2) \
101  ((PT_IS_NUMERIC_TYPE (typ1) && PT_IS_NUMERIC_TYPE (typ2)) \
102  || (PT_IS_NUMERIC_TYPE (typ1) && typ2 == PT_TYPE_MAYBE) \
103  || (typ1 == PT_TYPE_MAYBE && PT_IS_NUMERIC_TYPE (typ2)))
104 
105 /* Two types are comparable if they are NUMBER types or same CHAR type */
106 #define PT_ARE_COMPARABLE(typ1, typ2) \
107  ((typ1 == typ2) || PT_ARE_COMPARABLE_CHAR_TYPE (typ1, typ2) || PT_ARE_COMPARABLE_NUMERIC_TYPE (typ1, typ2))
108 
109 #define PT_IS_RECURSIVE_EXPRESSION(node) \
110  ((node)->node_type == PT_EXPR && (PT_IS_LEFT_RECURSIVE_EXPRESSION (node) || PT_IS_RIGHT_RECURSIVE_EXPRESSION (node)))
111 
112 #define PT_IS_LEFT_RECURSIVE_EXPRESSION(node) \
113  ((node)->info.expr.op == PT_GREATEST || (node)->info.expr.op == PT_LEAST || (node)->info.expr.op == PT_COALESCE)
114 
115 #define PT_IS_RIGHT_RECURSIVE_EXPRESSION(node) \
116  ((node)->info.expr.op == PT_CASE || (node)->info.expr.op == PT_DECODE)
117 
118 #define PT_IS_CAST_MAYBE(node) \
119  ((node)->node_type == PT_EXPR && (node)->info.expr.op == PT_CAST \
120  && (node)->info.expr.arg1 != NULL && (node)->info.expr.arg1->type_enum == PT_TYPE_MAYBE)
121 
122 #define PT_NODE_IS_SESSION_VARIABLE(node) \
123  ((((node) != NULL) && \
124  ((node)->node_type == PT_EXPR) && \
125  (((node)->info.expr.op == PT_EVALUATE_VARIABLE) || \
126  (((node)->info.expr.op == PT_CAST) && \
127  ((node)->info.expr.arg1 != NULL) && \
128  ((node)->info.expr.arg1->node_type == PT_EXPR) && \
129  ((node)->info.expr.arg1->info.expr.op == PT_EVALUATE_VARIABLE)) \
130  )) ? true : false )
131 
133 {
138 
139 static COMPARE_BETWEEN_OPERATOR pt_Compare_between_operator_table[] = {
151 };
152 
153 #define COMPARE_BETWEEN_OPERATOR_COUNT \
154  sizeof(pt_Compare_between_operator_table) / \
155  sizeof(COMPARE_BETWEEN_OPERATOR)
156 
157 #define PT_COLL_WRAP_TYPE_FOR_MAYBE(type) \
158  ((PT_IS_CHAR_STRING_TYPE (type)) ? (type) : PT_TYPE_VARCHAR)
159 
160 /* maximum number of overloads for an expression */
161 #define MAX_OVERLOADS 16
162 
163 /* generic types */
165 {
167  PT_GENERIC_TYPE_STRING, /* any type of string */
168  PT_GENERIC_TYPE_STRING_VARYING, /* VARCHAR or VARNCHAR */
169  PT_GENERIC_TYPE_CHAR, /* VARCHAR or CHAR */
170  PT_GENERIC_TYPE_NCHAR, /* VARNCHAR or NCHAR */
171  PT_GENERIC_TYPE_BIT, /* BIT OR VARBIT */
172  PT_GENERIC_TYPE_DISCRETE_NUMBER, /* SMALLINT, INTEGER, BIGINTEGER */
173  PT_GENERIC_TYPE_NUMBER, /* any number type */
174  PT_GENERIC_TYPE_DATE, /* date, datetime or timestamp */
175  PT_GENERIC_TYPE_DATETIME, /* any date or time type */
176  PT_GENERIC_TYPE_SEQUENCE, /* any type of sequence */
177  PT_GENERIC_TYPE_LOB, /* BLOB or CLOB */
178  PT_GENERIC_TYPE_QUERY, /* Sub query (for range operators) */
179  PT_GENERIC_TYPE_PRIMITIVE, /* primitive types */
180  PT_GENERIC_TYPE_ANY /* any type */
182 
183 /* expression argument type description */
184 typedef union pt_arg_type_val
185 {
189 
190 /* expression argument type */
191 typedef struct pt_arg_type
192 {
195 } PT_ARG_TYPE;
196 
197 /* SQL expression signature */
198 typedef struct expression_signature
199 {
205 
206 /* SQL expression definition */
207 typedef struct expression_definition
208 {
213 
214 /* collation for a parse tree node */
215 typedef enum collation_result
216 {
221 
222 static PT_TYPE_ENUM pt_infer_common_type (const PT_OP_TYPE op, PT_TYPE_ENUM * arg1, PT_TYPE_ENUM * arg2,
223  PT_TYPE_ENUM * arg3, const TP_DOMAIN * expected_domain);
224 static bool pt_get_expression_definition (const PT_OP_TYPE op, EXPRESSION_DEFINITION * def);
225 static bool does_op_specially_treat_null_arg (PT_OP_TYPE op);
226 static int pt_apply_expressions_definition (PARSER_CONTEXT * parser, PT_NODE ** expr);
227 static PT_TYPE_ENUM pt_expr_get_return_type (PT_NODE * expr, const EXPRESSION_SIGNATURE sig);
228 static int pt_coerce_expression_argument (PARSER_CONTEXT * parser, PT_NODE * expr, PT_NODE ** arg,
229  const PT_TYPE_ENUM arg_type, PT_NODE * data_type);
230 static PT_NODE *pt_coerce_expr_arguments (PARSER_CONTEXT * parser, PT_NODE * expr, PT_NODE * arg1, PT_NODE * arg2,
231  PT_NODE * arg3, EXPRESSION_SIGNATURE sig);
232 static PT_NODE *pt_coerce_range_expr_arguments (PARSER_CONTEXT * parser, PT_NODE * expr, PT_NODE * arg1, PT_NODE * arg2,
233  PT_NODE * arg3, EXPRESSION_SIGNATURE sig);
234 static bool pt_is_range_expression (const PT_OP_TYPE op);
235 static bool pt_are_equivalent_types (const PT_ARG_TYPE def_type, const PT_TYPE_ENUM op_type);
236 static bool pt_are_unmatchable_types (const PT_ARG_TYPE def_type, const PT_TYPE_ENUM op_type);
237 static PT_TYPE_ENUM pt_get_equivalent_type (const PT_ARG_TYPE def_type, const PT_TYPE_ENUM type);
238 static PT_TYPE_ENUM pt_get_equivalent_type_with_op (const PT_ARG_TYPE def_type, const PT_TYPE_ENUM arg_type,
239  PT_OP_TYPE op);
240 static PT_NODE *pt_evaluate_new_data_type (const PT_TYPE_ENUM old_type, const PT_TYPE_ENUM new_type,
241  PT_NODE * data_type);
242 static PT_TYPE_ENUM pt_get_common_collection_type (const PT_NODE * set, bool * is_multitype);
243 static bool pt_is_collection_of_type (const PT_NODE * collection, const PT_TYPE_ENUM collection_type,
244  const PT_TYPE_ENUM element_type);
245 static bool pt_is_symmetric_type (const PT_TYPE_ENUM type_enum);
246 static PT_NODE *pt_propagate_types (PARSER_CONTEXT * parser, PT_NODE * expr, PT_NODE * otype1, PT_NODE * otype2);
247 static int pt_union_sets (PARSER_CONTEXT * parser, TP_DOMAIN * domain, DB_VALUE * set1, DB_VALUE * set2,
248  DB_VALUE * result, PT_NODE * o2);
249 static int pt_difference_sets (PARSER_CONTEXT * parser, TP_DOMAIN * domain, DB_VALUE * set1, DB_VALUE * set2,
250  DB_VALUE * result, PT_NODE * o2);
251 static int pt_product_sets (PARSER_CONTEXT * parser, TP_DOMAIN * domain, DB_VALUE * set1, DB_VALUE * set2,
252  DB_VALUE * result, PT_NODE * o2);
253 static PT_NODE *pt_to_false_subquery (PARSER_CONTEXT * parser, PT_NODE * node);
254 static PT_NODE *pt_eval_recursive_expr_type (PARSER_CONTEXT * parser, PT_NODE * gl_expr);
255 static PT_NODE *pt_eval_type_pre (PARSER_CONTEXT * parser, PT_NODE * node, void *arg, int *continue_walk);
256 static PT_NODE *pt_eval_type (PARSER_CONTEXT * parser, PT_NODE * node, void *arg, int *continue_walk);
257 static PT_NODE *pt_fold_constants (PARSER_CONTEXT * parser, PT_NODE * node, void *arg, int *continue_walk);
258 static void pt_chop_to_one_select_item (PARSER_CONTEXT * parser, PT_NODE * node);
259 static bool pt_is_able_to_determine_return_type (const PT_OP_TYPE op);
260 static PT_NODE *pt_eval_expr_type (PARSER_CONTEXT * parser, PT_NODE * node);
261 static PT_NODE *pt_eval_opt_type (PARSER_CONTEXT * parser, PT_NODE * node);
262 static PT_TYPE_ENUM pt_common_type_op (PT_TYPE_ENUM t1, PT_OP_TYPE op, PT_TYPE_ENUM t2);
263 static int pt_upd_domain_info (PARSER_CONTEXT * parser, PT_NODE * arg1, PT_NODE * arg2, PT_OP_TYPE op,
264  PT_TYPE_ENUM common_type, PT_NODE * node);
265 static int pt_check_and_coerce_to_time (PARSER_CONTEXT * parser, PT_NODE * src);
266 static int pt_check_and_coerce_to_date (PARSER_CONTEXT * parser, PT_NODE * src);
267 static int pt_coerce_str_to_time_date_utime_datetime (PARSER_CONTEXT * parser, PT_NODE * src,
268  PT_TYPE_ENUM * result_type);
269 static int pt_coerce_3args (PARSER_CONTEXT * parser, PT_NODE * arg1, PT_NODE * arg2, PT_NODE * arg3);
270 static PT_NODE *pt_eval_function_type (PARSER_CONTEXT * parser, PT_NODE * node);
271 static PT_NODE *pt_eval_method_call_type (PARSER_CONTEXT * parser, PT_NODE * node);
272 static PT_NODE *pt_fold_const_expr (PARSER_CONTEXT * parser, PT_NODE * expr, void *arg);
273 static PT_NODE *pt_fold_const_function (PARSER_CONTEXT * parser, PT_NODE * func);
274 static const char *pt_class_name (const PT_NODE * type);
275 static int pt_set_default_data_type (PARSER_CONTEXT * parser, PT_TYPE_ENUM type, PT_NODE ** dtp);
276 static bool pt_is_explicit_coerce_allowed_for_default_value (PARSER_CONTEXT * parser, PT_TYPE_ENUM data_type,
277  PT_TYPE_ENUM desired_type);
278 static int pt_coerce_value_internal (PARSER_CONTEXT * parser, PT_NODE * src, PT_NODE * dest,
279  PT_TYPE_ENUM desired_type, PT_NODE * data_type, bool check_string_precision,
280  bool implicit_coercion);
281 #if defined(ENABLE_UNUSED_FUNCTION)
282 static int generic_func_casecmp (const void *a, const void *b);
283 static void init_generic_funcs (void);
284 #endif /* ENABLE_UNUSED_FUNCTION */
285 static PT_NODE *pt_compare_bounds_to_value (PARSER_CONTEXT * parser, PT_NODE * expr, PT_OP_TYPE op,
286  PT_TYPE_ENUM lhs_type, DB_VALUE * rhs_val, PT_TYPE_ENUM rhs_type);
287 static PT_TYPE_ENUM pt_get_common_datetime_type (PARSER_CONTEXT * parser, PT_TYPE_ENUM common_type,
288  PT_TYPE_ENUM arg1_type, PT_TYPE_ENUM arg2_type, PT_NODE * arg1,
289  PT_NODE * arg2);
290 static int pt_character_length_for_node (PT_NODE * node, const PT_TYPE_ENUM coerce_type);
291 static PT_NODE *pt_wrap_expr_w_exp_dom_cast (PARSER_CONTEXT * parser, PT_NODE * expr);
292 static bool pt_is_op_with_forced_common_type (PT_OP_TYPE op);
293 static bool pt_check_const_fold_op_w_args (PT_OP_TYPE op, DB_VALUE * arg1, DB_VALUE * arg2, DB_VALUE * arg3,
294  TP_DOMAIN * domain);
295 static bool pt_is_range_or_comp (PT_OP_TYPE op);
296 static bool pt_is_op_w_collation (const PT_OP_TYPE op);
297 static COLLATION_RESULT pt_get_collation_info_for_collection_type (PARSER_CONTEXT * parser, const PT_NODE * node,
298  PT_COLL_INFER * coll_infer);
299 static COLLATION_RESULT pt_get_collation_of_collection (PARSER_CONTEXT * parser, const PT_NODE * node,
300  PT_COLL_INFER * coll_infer, const bool is_inner_collection,
301  bool * is_first_element);
302 static PT_NODE *pt_coerce_node_collection_of_collection (PARSER_CONTEXT * parser, PT_NODE * node, const int coll_id,
303  const INTL_CODESET codeset, bool force_mode,
304  bool use_collate_modifier, PT_TYPE_ENUM wrap_type_for_maybe,
305  PT_TYPE_ENUM wrap_type_collection);
306 static PT_NODE *pt_coerce_node_collation (PARSER_CONTEXT * parser, PT_NODE * node, const int coll_id,
307  const INTL_CODESET codeset, bool force_mode, bool use_collate_modifier,
308  PT_TYPE_ENUM wrap_type_for_maybe, PT_TYPE_ENUM wrap_type_collection);
309 static int pt_check_expr_collation (PARSER_CONTEXT * parser, PT_NODE ** node);
310 static int pt_check_recursive_expr_collation (PARSER_CONTEXT * parser, PT_NODE ** node);
311 static PT_NODE *pt_node_to_enumeration_expr (PARSER_CONTEXT * parser, PT_NODE * data_type, PT_NODE * node);
312 static PT_NODE *pt_select_list_to_enumeration_expr (PARSER_CONTEXT * parser, PT_NODE * data_type, PT_NODE * node);
313 static bool pt_is_enumeration_special_comparison (PT_NODE * arg1, PT_OP_TYPE op, PT_NODE * arg2);
314 static PT_NODE *pt_fix_enumeration_comparison (PARSER_CONTEXT * parser, PT_NODE * expr);
315 static PT_TYPE_ENUM pt_get_common_arg_type_of_width_bucket (PARSER_CONTEXT * parser, PT_NODE * node);
316 static bool pt_is_const_foldable_width_bucket (PARSER_CONTEXT * parser, PT_NODE * expr);
317 static PT_TYPE_ENUM pt_wrap_type_for_collation (const PT_NODE * arg1, const PT_NODE * arg2, const PT_NODE * arg3,
318  PT_TYPE_ENUM * wrap_type_collection);
319 static void pt_fix_arguments_collation_flag (PT_NODE * expr);
320 static PT_NODE *pt_check_function_collation (PARSER_CONTEXT * parser, PT_NODE * node);
321 static void pt_hv_consistent_data_type_with_domain (PARSER_CONTEXT * parser, PT_NODE * node);
322 static void pt_update_host_var_data_type (PARSER_CONTEXT * parser, PT_NODE * hv_node);
323 static bool pt_cast_needs_wrap_for_collation (PT_NODE * node, const INTL_CODESET codeset);
324 
325 /*
326  * pt_get_expression_definition () - get the expression definition for the
327  * expression op.
328  * return: true if the expression has a definition, false otherwise
329  * op(in) : the expression operator
330  * def(in/out): the expression definition
331  */
332 static bool
333 pt_get_expression_definition (const PT_OP_TYPE op, EXPRESSION_DEFINITION * def)
334 {
336  int num;
337 
338  assert (def != NULL);
339 
340  def->op = op;
341 
342  sig.arg1_type.is_generic = false;
344 
345  sig.arg2_type.is_generic = false;
347 
348  sig.arg3_type.is_generic = false;
350 
351  sig.return_type.is_generic = false;
353 
354  switch (op)
355  {
356  case PT_AND:
357  case PT_OR:
358  case PT_XOR:
359  num = 0;
360 
361  /* one overload */
362 
363  /* arg1 */
364  sig.arg1_type.is_generic = false;
366 
367  /* arg2 */
368  sig.arg2_type.is_generic = false;
370 
371  /* return type */
372  sig.return_type.is_generic = false;
374 
375  def->overloads[num++] = sig;
376 
377  def->overloads_count = num;
378  break;
379 
380  case PT_NOT:
381  num = 0;
382 
383  /* one overload */
384 
385  /* arg1 */
386  sig.arg1_type.is_generic = false;
388 
389  /* return type */
390  sig.return_type.is_generic = false;
392 
393  def->overloads[num++] = sig;
394 
395  def->overloads_count = num;
396  break;
397 
398  case PT_ACOS:
399  case PT_ASIN:
400  case PT_ATAN:
401  case PT_COS:
402  case PT_COT:
403  case PT_DEGREES:
404  case PT_EXP:
405  case PT_LN:
406  case PT_LOG10:
407  case PT_LOG2:
408  case PT_SQRT:
409  case PT_RADIANS:
410  case PT_SIN:
411  case PT_TAN:
412  num = 0;
413 
414  /* one overload */
415 
416  /* arg1 */
417  sig.arg1_type.is_generic = false;
419 
420  /* return type */
421  sig.return_type.is_generic = false;
423 
424  def->overloads[num++] = sig;
425 
426  def->overloads_count = num;
427  break;
428 
429  case PT_ABS:
430  num = 0;
431 
432  /* one overload */
433 
434  /* arg1 */
435  sig.arg1_type.is_generic = true;
437 
438  /* return type */
439  sig.return_type.is_generic = true;
441 
442  def->overloads[num++] = sig;
443 
444  def->overloads_count = num;
445  break;
446 
447  case PT_ATAN2:
448  case PT_LOG:
449  case PT_POWER:
450  num = 0;
451 
452  /* one overload */
453 
454  /* arg1 */
455  sig.arg1_type.is_generic = false;
457 
458  /* arg2 */
459  sig.arg2_type.is_generic = false;
461 
462  /* return type */
463  sig.return_type.is_generic = false;
465 
466  def->overloads[num++] = sig;
467 
468  def->overloads_count = num;
469  break;
470 
471  case PT_RAND:
472  case PT_RANDOM:
473  num = 0;
474 
475  /* one overload */
476 
477  /* arg1 */
478  sig.arg1_type.is_generic = false;
480 
481  /* return type */
482  sig.return_type.is_generic = false;
484 
485  def->overloads[num++] = sig;
486 
487  /* arg1 */
488  sig.arg1_type.is_generic = false;
490 
491  /* return type */
492  sig.return_type.is_generic = false;
494 
495  def->overloads[num++] = sig;
496 
497  def->overloads_count = num;
498  break;
499 
500  case PT_DRAND:
501  case PT_DRANDOM:
502  num = 0;
503 
504  /* one overload */
505 
506  /* arg1 */
507  sig.arg1_type.is_generic = false;
509 
510  /* return type */
511  sig.return_type.is_generic = false;
513 
514  def->overloads[num++] = sig;
515 
516  /* arg1 */
517  sig.arg1_type.is_generic = false;
519 
520  /* return type */
521  sig.return_type.is_generic = false;
523 
524  def->overloads[num++] = sig;
525 
526  def->overloads_count = num;
527  break;
528 
529  case PT_BIT_AND:
530  case PT_BIT_XOR:
531  case PT_BIT_OR:
532  case PT_BITSHIFT_LEFT:
533  case PT_BITSHIFT_RIGHT:
534  num = 0;
535 
536  /* one overload */
537 
538  /* arg1 */
539  sig.arg1_type.is_generic = false;
541 
542  /* arg2 */
543  sig.arg2_type.is_generic = false;
545 
546  /* return type */
547  sig.return_type.is_generic = false;
549 
550  def->overloads[num++] = sig;
551 
552  def->overloads_count = num;
553  break;
554 
555  case PT_BIT_LENGTH:
556  case PT_OCTET_LENGTH:
557  num = 0;
558 
559  /* two overloads */
560 
561  /* arg1 */
562  sig.arg1_type.is_generic = true;
564  /* return type */
565 
566  sig.return_type.is_generic = false;
568  def->overloads[num++] = sig;
569 
570  /* arg1 */
571  sig.arg1_type.is_generic = true;
573 
574  /* return type */
575  sig.return_type.is_generic = false;
577  def->overloads[num++] = sig;
578 
579  def->overloads_count = num;
580  break;
581 
582  case PT_BIT_COUNT:
583  num = 0;
584 
585  /* one overload */
586 
587  /* arg1 */
588  sig.arg1_type.is_generic = true;
590 
591  /* return type */
592  sig.return_type.is_generic = false;
594 
595  def->overloads[num++] = sig;
596 
597  def->overloads_count = num;
598  break;
599 
600  case PT_BIT_NOT:
601  num = 0;
602 
603  /* one overload */
604 
605  /* arg1 */
606  sig.arg1_type.is_generic = false;
608 
609  /* return type */
610  sig.return_type.is_generic = false;
612 
613  def->overloads[num++] = sig;
614 
615  def->overloads_count = num;
616  break;
617 
618  case PT_BETWEEN:
619  case PT_NOT_BETWEEN:
620  num = 0;
621 
622  /* one overload */
623 
624  /* arg1 */
625  sig.arg1_type.is_generic = true;
627 
628  /* arg2 */
629  sig.arg2_type.is_generic = true;
631 
632  /* arg3 */
633  sig.arg3_type.is_generic = true;
635 
636  /* return type */
637  sig.return_type.is_generic = false;
639 
640  def->overloads[num++] = sig;
641 
642  def->overloads_count = num;
643  break;
644 
645  case PT_LIKE:
646  case PT_NOT_LIKE:
647  num = 0;
648 
649  /* four overloads */
650 
651  /* BOOL PT_LIKE([VAR]CHAR, [VAR]CHAR); */
652  sig.arg1_type.is_generic = true;
654 
655  sig.arg2_type.is_generic = true;
657 
658  sig.return_type.is_generic = false;
660 
661  def->overloads[num++] = sig;
662 
663  /* BOOL PT_LIKE([VAR]NCHAR, [VAR]NCHAR); */
664  sig.arg1_type.is_generic = true;
666 
667  sig.arg2_type.is_generic = true;
669 
670  sig.return_type.is_generic = false;
672 
673  def->overloads[num++] = sig;
674 
675  /* BOOL PT_LIKE([VAR]CHAR, [VAR]CHAR, [VAR]CHAR); */
676  sig.arg1_type.is_generic = true;
678 
679  sig.arg2_type.is_generic = true;
681 
682  sig.arg3_type.is_generic = true;
684 
685  sig.return_type.is_generic = false;
687 
688  def->overloads[num++] = sig;
689 
690  /* BOOL PT_LIKE([VAR]NCHAR, [VAR]NCHAR, [VAR]NCHAR); */
691  sig.arg1_type.is_generic = true;
693 
694  sig.arg2_type.is_generic = true;
696 
697  sig.arg3_type.is_generic = true;
699 
700  sig.return_type.is_generic = false;
702 
703  def->overloads[num++] = sig;
704 
705  def->overloads_count = num;
706  break;
707 
708  case PT_RLIKE:
709  case PT_NOT_RLIKE:
710  case PT_RLIKE_BINARY:
711  case PT_NOT_RLIKE_BINARY:
712  num = 0;
713 
714  /* two overloads */
715 
716  /* BOOL PT_RLIKE([VAR]CHAR, [VAR]CHAR, INT); */
717  sig.arg1_type.is_generic = true;
719 
720  sig.arg2_type.is_generic = true;
722 
723  sig.arg3_type.is_generic = false;
725 
726  sig.return_type.is_generic = false;
728 
729  def->overloads[num++] = sig;
730 
731  /* BOOL PT_RLIKE([VAR]NCHAR, [VAR]NCHAR, INT); */
732  sig.arg1_type.is_generic = true;
734 
735  sig.arg2_type.is_generic = true;
737 
738  sig.arg3_type.is_generic = false;
740 
741  sig.return_type.is_generic = false;
743 
744  def->overloads[num++] = sig;
745 
746  def->overloads_count = num;
747  break;
748 
749  case PT_CEIL:
750  case PT_FLOOR:
751  num = 0;
752 
753  /* one overload */
754 
755  /* arg1 */
756  sig.arg1_type.is_generic = true;
758 
759  /* return type */
760  sig.return_type.is_generic = true;
762 
763  def->overloads[num++] = sig;
764 
765  def->overloads_count = num;
766  break;
767 
768  case PT_CHR:
769  num = 0;
770 
771  /* one overload */
772 
773  /* arg1 */
774  sig.arg1_type.is_generic = true;
776 
777  /* arg2 */
778  sig.arg2_type.is_generic = false;
780 
781  /* return type */
782  sig.return_type.is_generic = false;
784 
785  def->overloads[num++] = sig;
786 
787  def->overloads_count = num;
788  break;
789 
790  case PT_CHAR_LENGTH:
791  num = 0;
792 
793  /* one overload */
794 
795  /* arg1 */
796  sig.arg1_type.is_generic = true;
798 
799  /* return type */
800  sig.return_type.is_generic = false;
802 
803  def->overloads[num++] = sig;
804 
805  def->overloads_count = num;
806  break;
807 
808  case PT_REPEAT:
809  num = 0;
810 
811  /* one overload */
812 
813  /* arg1 */
814  sig.arg1_type.is_generic = true;
816 
817  /* arg2 */
818  sig.arg2_type.is_generic = false;
820 
821  /* return type */
822  sig.return_type.is_generic = true;
824 
825  def->overloads[num++] = sig;
826 
827  def->overloads_count = num;
828  break;
829 
830  case PT_ADD_MONTHS:
831  num = 0;
832 
833  /* one overload */
834  /* arg1 */
835  sig.arg1_type.is_generic = false;
837 
838  /* arg2 */
839  sig.arg2_type.is_generic = false;
841 
842  /* return type */
843  sig.return_type.is_generic = false;
845 
846  def->overloads[num++] = sig;
847 
848  def->overloads_count = num;
849  break;
850 
851  case PT_FROMDAYS:
852  num = 0;
853 
854  /* two overload */
855 
856  sig.arg1_type.is_generic = true;
858  sig.return_type.is_generic = false;
860  def->overloads[num++] = sig;
861 
862  sig.arg1_type.is_generic = false;
864  sig.return_type.is_generic = false;
866  def->overloads[num++] = sig;
867 
868  def->overloads_count = num;
869  break;
870 
871  case PT_LOWER:
872  case PT_UPPER:
873  num = 0;
874 
875  /* one overload */
876 
877  /* arg1 */
878  sig.arg1_type.is_generic = true;
880 
881  /* return type */
882  sig.return_type.is_generic = true;
884 
885  def->overloads[num++] = sig;
886 
887  def->overloads_count = num;
888  break;
889 
890  case PT_HEX:
891  num = 0;
892 
893  /* three overloads */
894 
895  /* HEX (STRING) */
896  /* arg1 */
897  sig.arg1_type.is_generic = true;
899  /* return type */
900  sig.return_type.is_generic = false;
902  def->overloads[num++] = sig;
903 
904  /* HEX (NUMBER) */
905  /* arg1 */
906  sig.arg1_type.is_generic = true;
908  /* return type */
909  sig.return_type.is_generic = false;
911  def->overloads[num++] = sig;
912 
913  /* HEX (BIT) */
914  /* arg1 */
915  sig.arg1_type.is_generic = true;
917  /* return type */
918  sig.return_type.is_generic = false;
920  def->overloads[num++] = sig;
921 
922  def->overloads_count = num;
923  break;
924 
925  case PT_ASCII:
926  num = 0;
927 
928  /* two overloads */
929 
930  /* ASCII (STRING) */
931  /* arg1 */
932  sig.arg1_type.is_generic = true;
934  /* return type */
935  sig.return_type.is_generic = false;
937  def->overloads[num++] = sig;
938 
939  /* ASCII (BIT) */
940  /* arg1 */
941  sig.arg1_type.is_generic = true;
943  /* return type */
944  sig.return_type.is_generic = false;
946  def->overloads[num++] = sig;
947 
948  def->overloads_count = num;
949  break;
950 
951  case PT_CONV:
952  num = 0;
953 
954  /* three overloads */
955 
956  /* CONV(NUMBER, SMALLINT, SMALLINT) */
957  /* arg1 */
958  sig.arg1_type.is_generic = true;
960  /* arg2 */
961  sig.arg2_type.is_generic = false;
963  /* arg3 */
964  sig.arg3_type.is_generic = false;
966  /* return type */
967  sig.return_type.is_generic = false;
969  def->overloads[num++] = sig;
970 
971  /* CONV(VARCHAR, SMALLINT, SMALLINT) */
972  /* arg1 */
973  sig.arg1_type.is_generic = true;
975  /* arg2 */
976  sig.arg2_type.is_generic = false;
978  /* arg3 */
979  sig.arg3_type.is_generic = false;
981  /* return type */
982  sig.return_type.is_generic = false;
984  def->overloads[num++] = sig;
985 
986  /* CONV(BIT, SMALLINT, SMALLINT) */
987  /* arg1 */
988  sig.arg1_type.is_generic = true;
990  /* arg2 */
991  sig.arg2_type.is_generic = false;
993  /* arg3 */
994  sig.arg3_type.is_generic = false;
996  /* return type */
997  sig.return_type.is_generic = false;
999  def->overloads[num++] = sig;
1000 
1001  def->overloads_count = num;
1002  break;
1003 
1004  case PT_DATEF:
1005  case PT_REVERSE:
1006  num = 0;
1007 
1008  /* one overload */
1009 
1010  /* arg1 */
1011  sig.arg1_type.is_generic = true;
1013 
1014  /* return type */
1015  sig.return_type.is_generic = true;
1017 
1018  def->overloads[num++] = sig;
1019 
1020  def->overloads_count = num;
1021  break;
1022  case PT_DISK_SIZE:
1023  num = 0;
1024 
1025  /* one overload */
1026 
1027  /* arg1 */
1028  sig.arg1_type.is_generic = true;
1030 
1031  /* return type */
1032  sig.return_type.is_generic = false;
1034 
1035  def->overloads[num++] = sig;
1036 
1037  def->overloads_count = num;
1038  break;
1039  case PT_LIKE_LOWER_BOUND:
1040  case PT_LIKE_UPPER_BOUND:
1041  num = 0;
1042 
1043  /* two overloads */
1044 
1045  /* arg1 */
1046  sig.arg1_type.is_generic = true;
1048  /* return type */
1049  sig.return_type.is_generic = false;
1051  def->overloads[num++] = sig;
1052 
1053  /* arg1 */
1054  sig.arg1_type.is_generic = true;
1056  /* arg2 */
1057  sig.arg2_type.is_generic = true;
1059  /* return type */
1060  sig.return_type.is_generic = false;
1062  def->overloads[num++] = sig;
1063 
1064  def->overloads_count = num;
1065  break;
1066 
1067  case PT_BIN:
1068  num = 0;
1069 
1070  /* one overload */
1071 
1072  /* arg1 */
1073  sig.arg1_type.is_generic = false;
1075  /* return type */
1076  sig.return_type.is_generic = false;
1078  def->overloads[num++] = sig;
1079 
1080  def->overloads_count = num;
1081  break;
1082 
1083  case PT_ADDTIME:
1084  num = 0;
1085 
1086  /* 12 overloads */
1087 
1088  /* arg1 */
1089  sig.arg1_type.is_generic = false;
1091  /* arg2 */
1092  sig.arg2_type.is_generic = false;
1094  /* return type */
1095  sig.return_type.is_generic = false;
1097  def->overloads[num++] = sig;
1098 
1099  sig.arg1_type.is_generic = false;
1101  /* arg2 */
1102  sig.arg2_type.is_generic = false;
1104  /* return type */
1105  sig.return_type.is_generic = false;
1107  def->overloads[num++] = sig;
1108 
1109  sig.arg1_type.is_generic = false;
1111  /* arg2 */
1112  sig.arg2_type.is_generic = false;
1114  /* return type */
1115  sig.return_type.is_generic = false;
1117  def->overloads[num++] = sig;
1118 
1119  /* arg1 */
1120  sig.arg1_type.is_generic = false;
1122  /* arg2 */
1123  sig.arg2_type.is_generic = false;
1125  /* return type */
1126  sig.return_type.is_generic = false;
1128  def->overloads[num++] = sig;
1129 
1130  /* arg1 */
1131  sig.arg1_type.is_generic = false;
1133  /* arg2 */
1134  sig.arg2_type.is_generic = false;
1136  /* return type */
1137  sig.return_type.is_generic = false;
1139  def->overloads[num++] = sig;
1140 
1141  /* arg1 */
1142  sig.arg1_type.is_generic = false;
1144  /* arg2 */
1145  sig.arg2_type.is_generic = false;
1147  /* return type */
1148  sig.return_type.is_generic = false;
1150  def->overloads[num++] = sig;
1151 
1152  /* arg1 */
1153  sig.arg1_type.is_generic = true;
1155  /* arg2 */
1156  sig.arg2_type.is_generic = true;
1158  /* return type */
1159  sig.return_type.is_generic = false;
1161  def->overloads[num++] = sig;
1162 
1163  /* arg1 */
1164  sig.arg1_type.is_generic = true;
1166  /* arg2 */
1167  sig.arg2_type.is_generic = false;
1169  /* return type */
1170  sig.return_type.is_generic = false;
1172  def->overloads[num++] = sig;
1173 
1174  /* arg1 */
1175  sig.arg1_type.is_generic = false;
1177  /* arg2 */
1178  sig.arg2_type.is_generic = false;
1180  /* return type */
1181  sig.return_type.is_generic = false;
1183  def->overloads[num++] = sig;
1184 
1185  /* arg1 */
1186  sig.arg1_type.is_generic = false;
1188  /* arg2 */
1189  sig.arg2_type.is_generic = false;
1191  /* return type */
1192  sig.return_type.is_generic = false;
1194  def->overloads[num++] = sig;
1195 
1196  /* arg1 */
1197  sig.arg1_type.is_generic = false;
1199  /* arg2 */
1200  sig.arg2_type.is_generic = true;
1202  /* return type */
1203  sig.return_type.is_generic = false;
1205  def->overloads[num++] = sig;
1206 
1207  /* arg1 */
1208  sig.arg1_type.is_generic = false;
1210  /* arg2 */
1211  sig.arg2_type.is_generic = false;
1213  /* return type */
1214  sig.return_type.is_generic = false;
1216  def->overloads[num++] = sig;
1217 
1218  def->overloads_count = num;
1219  break;
1220 
1221  case PT_TRIM:
1222  case PT_LTRIM:
1223  case PT_RTRIM:
1224  num = 0;
1225 
1226  /* two overloads */
1227 
1228  /* arg1 */
1229  sig.arg1_type.is_generic = true;
1231  /* return type */
1232  sig.return_type.is_generic = true;
1234  def->overloads[num++] = sig;
1235 
1236  /* arg1 */
1237  sig.arg1_type.is_generic = true;
1239  /* arg2 */
1240  sig.arg2_type.is_generic = true;
1242  /* return type */
1243  sig.return_type.is_generic = true;
1245  def->overloads[num++] = sig;
1246 
1247  def->overloads_count = num;
1248  break;
1249 
1250  case PT_MAKEDATE:
1251  num = 0;
1252 
1253  /* arg1 */
1254  sig.arg1_type.is_generic = true;
1256  /* arg2 */
1257  sig.arg2_type.is_generic = true;
1259  sig.return_type.is_generic = false;
1261  def->overloads[num++] = sig;
1262 
1263  /* arg1 */
1264  sig.arg1_type.is_generic = false;
1266  /* arg2 */
1267  sig.arg2_type.is_generic = false;
1269  /* return type */
1270  sig.return_type.is_generic = false;
1272  def->overloads[num++] = sig;
1273 
1274  def->overloads_count = num;
1275  break;
1276 
1277  case PT_MAKETIME:
1278  num = 0;
1279 
1280  /* two overloads */
1281 
1282  /* arg1 */
1283  sig.arg1_type.is_generic = true;
1285  /* arg2 */
1286  sig.arg2_type.is_generic = true;
1288  /* arg3 */
1289  sig.arg3_type.is_generic = true;
1291  /* return type */
1292  sig.return_type.is_generic = false;
1294  def->overloads[num++] = sig;
1295 
1296  /* arg1 */
1297  sig.arg1_type.is_generic = false;
1299  /* arg2 */
1300  sig.arg2_type.is_generic = false;
1302  /* arg3 */
1303  sig.arg3_type.is_generic = false;
1305  /* return type */
1306  sig.return_type.is_generic = false;
1308  def->overloads[num++] = sig;
1309 
1310  def->overloads_count = num;
1311  break;
1312 
1313  case PT_SECTOTIME:
1314  num = 0;
1315 
1316  /* two overloads */
1317 
1318  /* arg1 */
1319  sig.arg1_type.is_generic = true;
1321  /* return type */
1322  sig.return_type.is_generic = false;
1324  def->overloads[num++] = sig;
1325 
1326  /* arg1 */
1327  sig.arg1_type.is_generic = false;
1329  /* return type */
1330  sig.return_type.is_generic = false;
1332  def->overloads[num++] = sig;
1333 
1334  def->overloads_count = num;
1335  break;
1336 
1337  case PT_YEARF:
1338  case PT_DAYF:
1339  case PT_MONTHF:
1340  case PT_DAYOFMONTH:
1341  case PT_DAYOFWEEK:
1342  case PT_DAYOFYEAR:
1343  case PT_QUARTERF:
1344  case PT_TODAYS:
1345  case PT_WEEKDAY:
1346  num = 0;
1347 
1348  /* two overloads */
1349 
1350  /* arg1 */
1351  sig.arg1_type.is_generic = true;
1353  /* return type */
1354  sig.return_type.is_generic = false;
1356  def->overloads[num++] = sig;
1357 
1358  /* arg1 */
1359  sig.arg1_type.is_generic = true;
1361  /* return type */
1362  sig.return_type.is_generic = false;
1364  def->overloads[num++] = sig;
1365 
1366  def->overloads_count = num;
1367  break;
1368 
1369  case PT_LAST_DAY:
1370  num = 0;
1371 
1372  /* one overload */
1373 
1374  /* arg1 */
1375  sig.arg1_type.is_generic = false;
1377 
1378  /* return type */
1379  sig.return_type.is_generic = false;
1381 
1382  def->overloads[num++] = sig;
1383 
1384  def->overloads_count = num;
1385  break;
1386 
1387  case PT_CONCAT:
1389  num = 0;
1390 
1391  /* two overloads */
1392 
1393  /* arg1 */
1394  sig.arg1_type.is_generic = true;
1396  /* arg2 */
1397  sig.arg2_type.is_generic = true;
1399  /* return type */
1400  sig.return_type.is_generic = true;
1402  def->overloads[num++] = sig;
1403 
1404  /* arg1 */
1405  sig.arg1_type.is_generic = true;
1407  /* arg2 */
1408  sig.arg2_type.is_generic = true;
1410  /* return type */
1411  sig.return_type.is_generic = true;
1413  def->overloads[num++] = sig;
1414 
1415  def->overloads_count = num;
1416  break;
1417 
1418  case PT_CONCAT_WS:
1419  num = 0;
1420 
1421  /* two overloads */
1422 
1423  /* arg1 */
1424  sig.arg1_type.is_generic = true;
1426  /* arg2 */
1427  sig.arg2_type.is_generic = true;
1429  /* arg2 */
1430  sig.arg3_type.is_generic = true;
1432  /* return type */
1433  sig.return_type.is_generic = true;
1435  def->overloads[num++] = sig;
1436 
1437  /* arg1 */
1438  sig.arg1_type.is_generic = true;
1440  /* arg2 */
1441  sig.arg2_type.is_generic = true;
1443  /* arg2 */
1444  sig.arg3_type.is_generic = true;
1446  /* return type */
1447  sig.return_type.is_generic = true;
1449  def->overloads[num++] = sig;
1450 
1451  def->overloads_count = num;
1452  break;
1453 
1454  case PT_DATABASE:
1455  case PT_SCHEMA:
1456  case PT_VERSION:
1457  case PT_CURRENT_USER:
1458  case PT_LIST_DBS:
1459  case PT_SYS_GUID:
1460  case PT_USER:
1461  num = 0;
1462 
1463  /* one overload */
1464 
1465  /* no arguments, just a return type */
1466  sig.return_type.is_generic = false;
1468 
1469  def->overloads[num++] = sig;
1470 
1471  def->overloads_count = num;
1472  break;
1473 
1475  num = 0;
1476 
1477  /* one overload */
1478 
1479  /* no arguments, just a return type */
1480  sig.return_type.is_generic = false;
1482 
1483  def->overloads[num++] = sig;
1484 
1485  def->overloads_count = num;
1486  break;
1487 
1488  case PT_CURRENT_VALUE:
1489  num = 0;
1490 
1491  /* one overload */
1492 
1493  sig.arg1_type.is_generic = true;
1495 
1496  sig.return_type.is_generic = false;
1498 
1499  def->overloads[num++] = sig;
1500 
1501  def->overloads_count = num;
1502  break;
1503 
1504  case PT_NEXT_VALUE:
1505  num = 0;
1506 
1507  /* one overload */
1508 
1509  /* arg1 */
1510  sig.arg1_type.is_generic = true;
1512 
1513  /* arg2 */
1514  sig.arg2_type.is_generic = false;
1516 
1517  sig.return_type.is_generic = false;
1519 
1520  def->overloads[num++] = sig;
1521 
1522  def->overloads_count = num;
1523  break;
1524 
1525  case PT_DATE_FORMAT:
1526  num = 0;
1527 
1528  /* two overloads */
1529 
1530  /* arg1 */
1531  sig.arg1_type.is_generic = true;
1533  /* arg2 */
1534  sig.arg2_type.is_generic = true;
1536  /* arg3 */
1537  sig.arg3_type.is_generic = false;
1539  /* return type */
1540  sig.return_type.is_generic = false;
1542  def->overloads[num++] = sig;
1543 
1544  /* arg1 */
1545  sig.arg1_type.is_generic = true;
1547  /* arg2 */
1548  sig.arg2_type.is_generic = true;
1550  /* arg3 */
1551  sig.arg3_type.is_generic = false;
1553  /* return type */
1554  sig.return_type.is_generic = false;
1556  def->overloads[num++] = sig;
1557 
1558  def->overloads_count = num;
1559  break;
1560 
1561  case PT_DIV:
1562  case PT_MOD:
1563  num = 0;
1564 
1565  /* one overload */
1566 
1567  /* arg1 */
1568  sig.arg1_type.is_generic = true;
1570  /* arg2 */
1571  sig.arg2_type.is_generic = true;
1573 
1574  sig.return_type.is_generic = true;
1576  def->overloads[num++] = sig;
1577 
1578  def->overloads_count = num;
1579  break;
1580 
1581  case PT_DIVIDE:
1582  case PT_MODULUS:
1583  num = 0;
1584 
1585  /* one overload */
1586 
1587  /* arg1 */
1588  sig.arg1_type.is_generic = true;
1590  /* arg2 */
1591  sig.arg2_type.is_generic = true;
1593 
1594  sig.return_type.is_generic = true;
1596  def->overloads[num++] = sig;
1597 
1598  def->overloads_count = num;
1599  break;
1600 
1601  case PT_TIMES:
1602  num = 0;
1603 
1604  /* two overloads */
1605 
1606  /* arg1 */
1607  sig.arg1_type.is_generic = true;
1609  /* arg2 */
1610  sig.arg2_type.is_generic = true;
1612 
1613  sig.return_type.is_generic = true;
1615  def->overloads[num++] = sig;
1616 
1617  /* arg1 */
1618  sig.arg1_type.is_generic = true;
1620  /* arg2 */
1621  sig.arg2_type.is_generic = true;
1623 
1624  sig.return_type.is_generic = false;
1626  def->overloads[num++] = sig;
1627 
1628  def->overloads_count = num;
1629  break;
1630 
1631  case PT_PLUS:
1632  num = 0;
1633 
1634  /* number + number */
1635  sig.arg1_type.is_generic = true;
1637  sig.arg2_type.is_generic = true;
1639  sig.return_type.is_generic = true;
1641  def->overloads[num++] = sig;
1642 
1644  {
1645  /* char + char */
1646  sig.arg1_type.is_generic = true;
1648  sig.arg2_type.is_generic = true;
1650  sig.return_type.is_generic = true;
1652  def->overloads[num++] = sig;
1653 
1654  /* nchar + nchar */
1655  sig.arg1_type.is_generic = true;
1657  sig.arg2_type.is_generic = true;
1659  sig.return_type.is_generic = true;
1661  def->overloads[num++] = sig;
1662 
1663  /* bit + bit */
1664  sig.arg1_type.is_generic = true;
1666  sig.arg2_type.is_generic = true;
1668  sig.return_type.is_generic = true;
1670  def->overloads[num++] = sig;
1671  }
1672 
1673  /* collection + collection */
1674  sig.arg1_type.is_generic = true;
1676  sig.arg2_type.is_generic = true;
1678  sig.return_type.is_generic = true;
1680  def->overloads[num++] = sig;
1681 
1682  def->overloads_count = num;
1683 
1684  break;
1685 
1686  case PT_MINUS:
1687  num = 0;
1688 
1689  /* 4 overloads */
1690 
1691  sig.arg1_type.is_generic = true;
1693  sig.arg2_type.is_generic = true;
1695  sig.return_type.is_generic = true;
1697  def->overloads[num++] = sig;
1698 
1699  sig.arg1_type.is_generic = true;
1701  sig.arg2_type.is_generic = true;
1703  sig.return_type.is_generic = false;
1705  def->overloads[num++] = sig;
1706 
1707  sig.arg1_type.is_generic = false;
1709  sig.arg2_type.is_generic = false;
1711  sig.return_type.is_generic = false;
1713  def->overloads[num++] = sig;
1714 
1715  sig.arg1_type.is_generic = true;
1717  sig.arg2_type.is_generic = true;
1719  sig.return_type.is_generic = true;
1721  def->overloads[num++] = sig;
1722 
1723  def->overloads_count = num;
1724  break;
1725 
1726  case PT_HOURF:
1727  case PT_MINUTEF:
1728  case PT_SECONDF:
1729  case PT_TIMETOSEC:
1730  num = 0;
1731 
1732  /* 2 overloads */
1733 
1734  sig.arg1_type.is_generic = true;
1736  sig.return_type.is_generic = false;
1738  def->overloads[num++] = sig;
1739 
1740  sig.arg1_type.is_generic = false;
1742  sig.return_type.is_generic = false;
1744  def->overloads[num++] = sig;
1745 
1746  def->overloads_count = num;
1747  break;
1748 
1749  case PT_INSTR:
1750  num = 0;
1751 
1752  /* two overloads */
1753 
1754  /* arg1 */
1755  sig.arg1_type.is_generic = true;
1757  /* arg2 */
1758  sig.arg2_type.is_generic = true;
1760  /* arg3 */
1761  sig.arg3_type.is_generic = false;
1763  /* return type */
1764  sig.return_type.is_generic = false;
1766  def->overloads[num++] = sig;
1767 
1768  /* arg1 */
1769  sig.arg1_type.is_generic = true;
1771  /* arg2 */
1772  sig.arg2_type.is_generic = true;
1774  /* arg3 */
1775  sig.arg3_type.is_generic = false;
1777  /* return type */
1778  sig.return_type.is_generic = false;
1780  def->overloads[num++] = sig;
1781 
1782  def->overloads_count = num;
1783  break;
1784 
1785  case PT_LEFT:
1786  case PT_RIGHT:
1787  num = 0;
1788 
1789  /* two overloads */
1790 
1791  /* arg1 */
1792  sig.arg1_type.is_generic = true;
1794  /* arg2 */
1795  sig.arg2_type.is_generic = false;
1797 
1798  /* return type */
1799  sig.return_type.is_generic = false;
1801  def->overloads[num++] = sig;
1802 
1803  /* arg1 */
1804  sig.arg1_type.is_generic = true;
1806  /* arg2 */
1807  sig.arg2_type.is_generic = false;
1809 
1810  /* return type */
1811  sig.return_type.is_generic = false;
1813  def->overloads[num++] = sig;
1814 
1815  def->overloads_count = num;
1816  break;
1817 
1818  case PT_LOCATE:
1819  num = 0;
1820 
1821  /* four overloads */
1822 
1823  /* arg1 */
1824  sig.arg1_type.is_generic = true;
1826  /* arg2 */
1827  sig.arg2_type.is_generic = true;
1829  /* arg3 */
1830  sig.arg3_type.is_generic = false;
1832 
1833  /* return type */
1834  sig.return_type.is_generic = false;
1836  def->overloads[num++] = sig;
1837 
1838  /* arg1 */
1839  sig.arg1_type.is_generic = true;
1841  /* arg2 */
1842  sig.arg2_type.is_generic = true;
1844  /* arg3 */
1845  sig.arg3_type.is_generic = false;
1847 
1848  /* return type */
1849  sig.return_type.is_generic = false;
1851  def->overloads[num++] = sig;
1852 
1853  /* arg1 */
1854  sig.arg1_type.is_generic = true;
1856  /* arg2 */
1857  sig.arg2_type.is_generic = true;
1859  /* arg3 */
1860  sig.arg3_type.is_generic = false;
1862 
1863  /* return type */
1864  sig.return_type.is_generic = false;
1866  def->overloads[num++] = sig;
1867 
1868  /* arg1 */
1869  sig.arg1_type.is_generic = true;
1871  /* arg2 */
1872  sig.arg2_type.is_generic = true;
1874  /* arg3 */
1875  sig.arg3_type.is_generic = false;
1877 
1878  /* return type */
1879  sig.return_type.is_generic = false;
1881  def->overloads[num++] = sig;
1882 
1883  def->overloads_count = num;
1884  break;
1885 
1886  case PT_POSITION:
1887  case PT_STRCMP:
1888  case PT_FINDINSET:
1889  num = 0;
1890 
1891  /* two overloads */
1892 
1893  /* arg1 */
1894  sig.arg1_type.is_generic = true;
1896  /* arg2 */
1897  sig.arg2_type.is_generic = true;
1899 
1900  /* return type */
1901  sig.return_type.is_generic = false;
1903  def->overloads[num++] = sig;
1904 
1905  /* arg1 */
1906  sig.arg1_type.is_generic = true;
1908  /* arg2 */
1909  sig.arg2_type.is_generic = true;
1911 
1912  /* return type */
1913  sig.return_type.is_generic = false;
1915  def->overloads[num++] = sig;
1916 
1917  def->overloads_count = num;
1918  break;
1919 
1920  case PT_SUBSTRING_INDEX:
1921  num = 0;
1922 
1923  /* two overloads */
1924 
1925  /* arg1 */
1926  sig.arg1_type.is_generic = true;
1928  /* arg2 */
1929  sig.arg2_type.is_generic = true;
1931  /* arg3 */
1932  sig.arg3_type.is_generic = false;
1934 
1935  /* return type */
1936  sig.return_type.is_generic = false;
1938  def->overloads[num++] = sig;
1939 
1940  /* arg1 */
1941  sig.arg1_type.is_generic = true;
1943  /* arg2 */
1944  sig.arg2_type.is_generic = true;
1946  /* arg3 */
1947  sig.arg3_type.is_generic = false;
1949 
1950  /* return type */
1951  sig.return_type.is_generic = false;
1953  def->overloads[num++] = sig;
1954 
1955  def->overloads_count = num;
1956  break;
1957 
1958  case PT_LPAD:
1959  case PT_RPAD:
1960  num = 0;
1961 
1962  /* two overloads */
1963 
1964  /* arg1 */
1965  sig.arg1_type.is_generic = true;
1967 
1968  /* arg2 */
1969  sig.arg2_type.is_generic = false;
1971 
1972  /* arg3 */
1973  sig.arg3_type.is_generic = true;
1975 
1976  /* return type */
1977  sig.return_type.is_generic = false;
1979  def->overloads[num++] = sig;
1980 
1981  /* arg1 */
1982  sig.arg1_type.is_generic = true;
1984 
1985  /* arg2 */
1986  sig.arg2_type.is_generic = false;
1988 
1989  /* arg3 */
1990  sig.arg3_type.is_generic = true;
1992 
1993  /* return type */
1994  sig.return_type.is_generic = false;
1996  def->overloads[num++] = sig;
1997 
1998  def->overloads_count = num;
1999  break;
2000 
2001  case PT_MD5:
2002  num = 0;
2003 
2004  /* one overload */
2005 
2006  /* arg1 */
2007  sig.arg1_type.is_generic = true;
2009 
2010  /* return type */
2011  sig.return_type.is_generic = false;
2013  def->overloads[num++] = sig;
2014 
2015  def->overloads_count = num;
2016  break;
2017  case PT_SHA_ONE:
2018  num = 0;
2019 
2020  /* one overload */
2021 
2022  /* arg1 */
2023  sig.arg1_type.is_generic = true;
2025 
2026  /* return type */
2027  sig.return_type.is_generic = false;
2029  def->overloads[num++] = sig;
2030 
2031  def->overloads_count = num;
2032  break;
2033 
2034  case PT_SHA_TWO:
2035  num = 0;
2036 
2037  /* one overload */
2038 
2039  /* arg1 */
2040  sig.arg1_type.is_generic = true;
2042 
2043  /* arg2 */
2044  sig.arg2_type.is_generic = true;
2046 
2047  /* return type */
2048  sig.return_type.is_generic = false;
2050  def->overloads[num++] = sig;
2051 
2052  def->overloads_count = num;
2053  break;
2054 
2055  case PT_AES_ENCRYPT:
2056  num = 0;
2057 
2058  /* one overload */
2059 
2060  /* arg1 */
2061  sig.arg1_type.is_generic = true;
2063 
2064  /* arg2 */
2065  sig.arg2_type.is_generic = true;
2067 
2068  /* return type */
2069  sig.return_type.is_generic = false;
2071  def->overloads[num++] = sig;
2072 
2073  def->overloads_count = num;
2074  break;
2075 
2076  case PT_AES_DECRYPT:
2077  num = 0;
2078 
2079  /* one overload */
2080 
2081  /* arg1 */
2082  sig.arg1_type.is_generic = true;
2084 
2085  /* arg2 */
2086  sig.arg2_type.is_generic = true;
2088 
2089  /* return type */
2090  sig.return_type.is_generic = false;
2092  def->overloads[num++] = sig;
2093 
2094  def->overloads_count = num;
2095  break;
2096 
2097  case PT_TO_BASE64:
2098  case PT_FROM_BASE64:
2099  num = 0;
2100 
2101  /* one overload */
2102 
2103  /* arg1 */
2104  sig.arg1_type.is_generic = true;
2106 
2107  /* return type */
2108  sig.return_type.is_generic = false;
2110  def->overloads[num++] = sig;
2111 
2112  def->overloads_count = num;
2113  break;
2114 
2115  case PT_MID:
2116  num = 0;
2117 
2118  /* one overload */
2119 
2120  /* arg1 */
2121  sig.arg1_type.is_generic = true;
2123 
2124  /* arg2 */
2125  sig.arg2_type.is_generic = false;
2127 
2128  /* arg3 */
2129  sig.arg3_type.is_generic = false;
2131 
2132  /* return type */
2133  sig.return_type.is_generic = true;
2135  def->overloads[num++] = sig;
2136 
2137  def->overloads_count = num;
2138  break;
2139 
2140  case PT_SUBSTRING:
2141  num = 0;
2142 
2143  /* two overloads */
2144 
2145  /* SUBSTRING (string, int, int) */
2146  sig.arg1_type.is_generic = true;
2148  sig.arg2_type.is_generic = false;
2150  sig.arg3_type.is_generic = false;
2152  sig.return_type.is_generic = true;
2154  def->overloads[num++] = sig;
2155 
2156  /* SUBSTRING (string, int) */
2157  sig.arg1_type.is_generic = true;
2159  sig.arg2_type.is_generic = false;
2161  sig.arg3_type.is_generic = false;
2163  sig.return_type.is_generic = true;
2165  def->overloads[num++] = sig;
2166 
2167  def->overloads_count = num;
2168  break;
2169 
2170  case PT_MONTHS_BETWEEN:
2171  num = 0;
2172 
2173  /* one overload */
2174 
2175  /* arg1 */
2176  sig.arg1_type.is_generic = false;
2178 
2179  /* arg2 */
2180  sig.arg2_type.is_generic = false;
2182 
2183  /* return type */
2184  sig.return_type.is_generic = false;
2186  def->overloads[num++] = sig;
2187 
2188  def->overloads_count = num;
2189  break;
2190 
2191  case PT_PI:
2192  num = 0;
2193 
2194  /* one overload */
2195 
2196  /* return type */
2197  sig.return_type.is_generic = false;
2199  def->overloads[num++] = sig;
2200 
2201  def->overloads_count = num;
2202  break;
2203 
2204  case PT_REPLACE:
2205  case PT_TRANSLATE:
2206  num = 0;
2207 
2208  /* two overloads */
2209 
2210  /* arg1 */
2211  sig.arg1_type.is_generic = true;
2213  /* arg2 */
2214  sig.arg2_type.is_generic = true;
2216  /* arg3 */
2217  sig.arg3_type.is_generic = true;
2219  /* return type */
2220  sig.return_type.is_generic = false;
2222  def->overloads[num++] = sig;
2223 
2224  /* arg1 */
2225  sig.arg1_type.is_generic = true;
2227  /* arg2 */
2228  sig.arg2_type.is_generic = true;
2230  /* arg3 */
2231  sig.arg3_type.is_generic = true;
2233  /* return type */
2234  sig.return_type.is_generic = false;
2236  def->overloads[num++] = sig;
2237 
2238  def->overloads_count = num;
2239  break;
2240 
2241  case PT_SPACE:
2242  num = 0;
2243 
2244  /* one overload */
2245 
2246  /* arg1 */
2247  sig.arg1_type.is_generic = true;
2249 
2250  /* return type */
2251  sig.return_type.is_generic = false;
2253  def->overloads[num++] = sig;
2254 
2255  def->overloads_count = num;
2256  break;
2257 
2258  case PT_STRCAT:
2259  num = 0;
2260 
2261  /* two overloads */
2262 
2263  /* arg1 */
2264  sig.arg1_type.is_generic = true;
2266 
2267  /* arg2 */
2268  sig.arg2_type.is_generic = true;
2270 
2271  /* return type */
2272  sig.return_type.is_generic = true;
2274  def->overloads[num++] = sig;
2275 
2276  /* arg1 */
2277  sig.arg1_type.is_generic = true;
2279 
2280  /* arg2 */
2281  sig.arg2_type.is_generic = true;
2283 
2284  /* return type */
2285  sig.return_type.is_generic = true;
2287  def->overloads[num++] = sig;
2288 
2289  def->overloads_count = num;
2290  break;
2291 
2292  case PT_UTC_DATE:
2293  case PT_SYS_DATE:
2294  case PT_CURRENT_DATE:
2295  num = 0;
2296 
2297  /* one overload */
2298 
2299  /* return type */
2300  sig.return_type.is_generic = false;
2302  def->overloads[num++] = sig;
2303 
2304  def->overloads_count = num;
2305  break;
2306 
2307  case PT_SYS_DATETIME:
2308  case PT_CURRENT_DATETIME:
2309  num = 0;
2310 
2311  /* one overload */
2312 
2313  /* return type */
2314  sig.return_type.is_generic = false;
2316  def->overloads[num++] = sig;
2317 
2318  def->overloads_count = num;
2319  break;
2320 
2321  case PT_UTC_TIME:
2322  case PT_SYS_TIME:
2323  case PT_CURRENT_TIME:
2324  num = 0;
2325 
2326  /* one overload */
2327 
2328  /* return type */
2329  sig.return_type.is_generic = false;
2331  def->overloads[num++] = sig;
2332 
2333  def->overloads_count = num;
2334  break;
2335 
2336  case PT_SYS_TIMESTAMP:
2337  case PT_UTC_TIMESTAMP:
2338  case PT_CURRENT_TIMESTAMP:
2339  num = 0;
2340 
2341  /* one overload */
2342 
2343  /* return type */
2344  sig.return_type.is_generic = false;
2346  def->overloads[num++] = sig;
2347 
2348  def->overloads_count = num;
2349  break;
2350 
2351  case PT_TIME_FORMAT:
2352  num = 0;
2353 
2354  /* three overloads */
2355 
2356  /* arg1 */
2357  sig.arg1_type.is_generic = true;
2359  /* arg2 */
2360  sig.arg2_type.is_generic = true;
2362  /* arg3 */
2363  sig.arg3_type.is_generic = false;
2365  /* return type */
2366  sig.return_type.is_generic = false;
2368  def->overloads[num++] = sig;
2369 
2370  /* arg1 */
2371  sig.arg1_type.is_generic = true;
2373  /* arg2 */
2374  sig.arg2_type.is_generic = true;
2376  /* arg3 */
2377  sig.arg3_type.is_generic = false;
2379  /* return type */
2380  sig.return_type.is_generic = false;
2382  def->overloads[num++] = sig;
2383 
2384  /* arg1 */
2385  sig.arg1_type.is_generic = true;
2387  /* arg2 */
2388  sig.arg2_type.is_generic = true;
2390  /* arg3 */
2391  sig.arg3_type.is_generic = false;
2393  /* return type */
2394  sig.return_type.is_generic = false;
2396  def->overloads[num++] = sig;
2397 
2398  def->overloads_count = num;
2399  break;
2400 
2401  case PT_TIMEF:
2402  num = 0;
2403 
2404  /* four overloads */
2405 
2406  /* arg1 */
2407  sig.arg1_type.is_generic = true;
2409  /* return type */
2410  sig.return_type.is_generic = true;
2412  def->overloads[num++] = sig;
2413 
2414  /* arg1 */
2415  sig.arg1_type.is_generic = false;
2417  /* return type */
2418  sig.return_type.is_generic = false;
2420  def->overloads[num++] = sig;
2421 
2422  /* arg1 */
2423  sig.arg1_type.is_generic = false;
2425  /* return type */
2426  sig.return_type.is_generic = false;
2428  def->overloads[num++] = sig;
2429 
2430  /* arg1 */
2431  sig.arg1_type.is_generic = false;
2433  /* return type */
2434  sig.return_type.is_generic = false;
2436  def->overloads[num++] = sig;
2437 
2438  def->overloads_count = num;
2439  break;
2440 
2441  case PT_TO_DATE:
2442  num = 0;
2443 
2444  /* one overload */
2445 
2446  /* arg1 */
2447  sig.arg1_type.is_generic = true;
2449  /* arg2 */
2450  sig.arg2_type.is_generic = true;
2452  /* arg3 */
2453  sig.arg3_type.is_generic = false;
2455 
2456  /* return type */
2457  sig.return_type.is_generic = false;
2459  def->overloads[num++] = sig;
2460 
2461  def->overloads_count = num;
2462  break;
2463 
2464  case PT_TO_DATETIME:
2465  num = 0;
2466 
2467  /* two overloads */
2468 
2469  /* arg1 */
2470  sig.arg1_type.is_generic = true;
2472  /* arg2 */
2473  sig.arg2_type.is_generic = true;
2475  /* arg3 */
2476  sig.arg3_type.is_generic = false;
2478 
2479  /* return type */
2480  sig.return_type.is_generic = false;
2482  def->overloads[num++] = sig;
2483 
2484  /* arg1 */
2485  sig.arg1_type.is_generic = true;
2487  /* arg2 */
2488  sig.arg2_type.is_generic = true;
2490  /* arg3 */
2491  sig.arg3_type.is_generic = false;
2493 
2494  /* return type */
2495  sig.return_type.is_generic = false;
2497  def->overloads[num++] = sig;
2498 
2499  def->overloads_count = num;
2500  break;
2501 
2502  case PT_TO_TIME:
2503  num = 0;
2504 
2505  /* two overloads */
2506 
2507  /* arg1 */
2508  sig.arg1_type.is_generic = true;
2510  /* arg2 */
2511  sig.arg2_type.is_generic = true;
2513  /* arg3 */
2514  sig.arg3_type.is_generic = false;
2516 
2517  /* return type */
2518  sig.return_type.is_generic = false;
2520  def->overloads[num++] = sig;
2521 
2522  /* arg1 */
2523  sig.arg1_type.is_generic = true;
2525  /* arg2 */
2526  sig.arg2_type.is_generic = true;
2528  /* arg3 */
2529  sig.arg3_type.is_generic = false;
2531 
2532  /* return type */
2533  sig.return_type.is_generic = false;
2535  def->overloads[num++] = sig;
2536 
2537  def->overloads_count = num;
2538  break;
2539 
2540  case PT_TO_TIMESTAMP:
2541  num = 0;
2542 
2543  /* two overloads */
2544 
2545  /* arg1 */
2546  sig.arg1_type.is_generic = true;
2548  /* arg2 */
2549  sig.arg2_type.is_generic = true;
2551  /* arg3 */
2552  sig.arg3_type.is_generic = false;
2554 
2555  /* return type */
2556  sig.return_type.is_generic = false;
2558  def->overloads[num++] = sig;
2559 
2560  /* arg1 */
2561  sig.arg1_type.is_generic = true;
2563  /* arg2 */
2564  sig.arg2_type.is_generic = true;
2566  /* arg3 */
2567  sig.arg3_type.is_generic = false;
2569 
2570  /* return type */
2571  sig.return_type.is_generic = false;
2573  def->overloads[num++] = sig;
2574 
2575  def->overloads_count = num;
2576  break;
2577 
2578  case PT_TO_NUMBER:
2579  num = 0;
2580 
2581  /* one overload */
2582 
2583  /* arg1 */
2584  sig.arg1_type.is_generic = true;
2586  /* arg2 */
2587  sig.arg2_type.is_generic = true;
2589  /* arg3 */
2590  sig.arg3_type.is_generic = false;
2592 
2593  /* return type */
2594  sig.return_type.is_generic = false;
2596  def->overloads[num++] = sig;
2597 
2598  def->overloads_count = num;
2599  break;
2600 
2601  case PT_WEEKF:
2602  num = 0;
2603 
2604  /* two overloads */
2605 
2606  /* arg1 */
2607  sig.arg1_type.is_generic = true;
2609  /* arg2 */
2610  sig.arg2_type.is_generic = true;
2612  /* return type */
2613  sig.return_type.is_generic = false;
2615  def->overloads[num++] = sig;
2616 
2617  /* arg1 */
2618  sig.arg1_type.is_generic = false;
2620  /* arg2 */
2621  sig.arg2_type.is_generic = false;
2623  /* return type */
2624  sig.return_type.is_generic = false;
2626  def->overloads[num++] = sig;
2627 
2628  def->overloads_count = num;
2629  break;
2630 
2631  case PT_CLOB_LENGTH:
2632  num = 0;
2633 
2634  /* one overload */
2635 
2636  /* arg1 */
2637  sig.arg1_type.is_generic = false;
2639 
2640  /* return type */
2641  sig.return_type.is_generic = false;
2643  def->overloads[num++] = sig;
2644 
2645  def->overloads_count = num;
2646  break;
2647 
2648  case PT_BLOB_LENGTH:
2649  num = 0;
2650 
2651  /* one overload */
2652 
2653  /* arg1 */
2654  sig.arg1_type.is_generic = false;
2656 
2657  /* return type */
2658  sig.return_type.is_generic = false;
2660  def->overloads[num++] = sig;
2661 
2662  def->overloads_count = num;
2663  break;
2664 
2665  case PT_BIT_TO_BLOB:
2666  num = 0;
2667 
2668  /* one overload */
2669 
2670  /* arg1 */
2671  sig.arg1_type.is_generic = true;
2673 
2674  /* return type */
2675  sig.return_type.is_generic = false;
2677  def->overloads[num++] = sig;
2678 
2679  def->overloads_count = num;
2680  break;
2681 
2682  case PT_CHAR_TO_CLOB:
2683  num = 0;
2684 
2685  /* one overload */
2686 
2687  /* arg1 */
2688  sig.arg1_type.is_generic = true;
2690 
2691  /* return type */
2692  sig.return_type.is_generic = false;
2694  def->overloads[num++] = sig;
2695 
2696  def->overloads_count = num;
2697  break;
2698 
2699  case PT_CHAR_TO_BLOB:
2700  num = 0;
2701 
2702  /* one overload */
2703 
2704  /* arg1 */
2705  sig.arg1_type.is_generic = true;
2707 
2708  /* return type */
2709  sig.return_type.is_generic = false;
2711  def->overloads[num++] = sig;
2712 
2713  def->overloads_count = num;
2714  break;
2715 
2716  case PT_BLOB_FROM_FILE:
2717  num = 0;
2718 
2719  /* one overload */
2720 
2721  /* arg1 */
2722  sig.arg1_type.is_generic = true;
2724 
2725  /* return type */
2726  sig.return_type.is_generic = false;
2728  def->overloads[num++] = sig;
2729 
2730  def->overloads_count = num;
2731  break;
2732 
2733  case PT_CLOB_FROM_FILE:
2734  num = 0;
2735 
2736  /* one overload */
2737 
2738  /* arg1 */
2739  sig.arg1_type.is_generic = true;
2741 
2742  /* return type */
2743  sig.return_type.is_generic = false;
2745  def->overloads[num++] = sig;
2746 
2747  def->overloads_count = num;
2748  break;
2749 
2750  case PT_BLOB_TO_BIT:
2751  num = 0;
2752 
2753  /* one overload */
2754 
2755  /* arg1 */
2756  sig.arg1_type.is_generic = false;
2758 
2759  /* return type */
2760  sig.return_type.is_generic = false;
2762  def->overloads[num++] = sig;
2763 
2764  def->overloads_count = num;
2765  break;
2766 
2767  case PT_CLOB_TO_CHAR:
2768  num = 0;
2769 
2770  /* one overload */
2771 
2772  /* arg1 */
2773  sig.arg1_type.is_generic = false;
2775 
2776  /* arg2 */
2777  sig.arg2_type.is_generic = false;
2779 
2780  /* return type */
2781  sig.return_type.is_generic = false;
2783  def->overloads[num++] = sig;
2784 
2785  def->overloads_count = num;
2786  break;
2787 
2788  case PT_INST_NUM:
2789  case PT_ROWNUM:
2790  case PT_ORDERBY_NUM:
2791  num = 0;
2792 
2793  /* one overload */
2794 
2795  /* return type */
2796  sig.return_type.is_generic = false;
2798  def->overloads[num++] = sig;
2799 
2800  def->overloads_count = num;
2801  break;
2802 
2803  case PT_LEVEL:
2804  case PT_CONNECT_BY_ISCYCLE:
2805  case PT_CONNECT_BY_ISLEAF:
2806  case PT_ROW_COUNT:
2807  num = 0;
2808 
2809  /* one overload */
2810 
2811  /* return type */
2812  sig.return_type.is_generic = false;
2814  def->overloads[num++] = sig;
2815 
2816  def->overloads_count = num;
2817  break;
2818 
2819  case PT_LAST_INSERT_ID:
2820  num = 0;
2821 
2822  /* one overload */
2823 
2824  /* return type */
2825  sig.return_type.is_generic = false;
2827  def->overloads[num++] = sig;
2828 
2829  def->overloads_count = num;
2830  break;
2831 
2832  case PT_DATEDIFF:
2833  num = 0;
2834 
2835  /* one overload */
2836 
2837  sig.arg1_type.is_generic = false;
2839 
2840  sig.arg2_type.is_generic = false;
2842 
2843  /* return type */
2844  sig.return_type.is_generic = false;
2846  def->overloads[num++] = sig;
2847 
2848  def->overloads_count = num;
2849  break;
2850 
2851  case PT_TIMEDIFF:
2852  num = 0;
2853 
2854  /* 3 overloads */
2855 
2856  /* arg1 */
2857  sig.arg1_type.is_generic = true;
2859  /* arg2 */
2860  sig.arg2_type.is_generic = true;
2862  /* return type */
2863  sig.return_type.is_generic = false;
2865  def->overloads[num++] = sig;
2866 
2867  /* arg1 */
2868  sig.arg1_type.is_generic = true;
2870  /* arg2 */
2871  sig.arg2_type.is_generic = true;
2873  /* return type */
2874  sig.return_type.is_generic = false;
2876  def->overloads[num++] = sig;
2877 
2878  /* arg1 */
2879  sig.arg1_type.is_generic = false;
2881  /* arg2 */
2882  sig.arg2_type.is_generic = false;
2884  /* return type */
2885  sig.return_type.is_generic = false;
2887  def->overloads[num++] = sig;
2888 
2889  def->overloads_count = num;
2890  break;
2891 
2892  case PT_INCR:
2893  case PT_DECR:
2894  num = 0;
2895 
2896  /* two overloads */
2897 
2898  sig.arg1_type.is_generic = true;
2900  /* return type */
2901  sig.return_type.is_generic = true;
2903  def->overloads[num++] = sig;
2904 
2905  sig.arg1_type.is_generic = true;
2907  sig.arg2_type.is_generic = false;
2909  sig.arg3_type.is_generic = true;
2911  /* return type */
2912  sig.return_type.is_generic = true;
2914  def->overloads[num++] = sig;
2915 
2916  def->overloads_count = num;
2917  break;
2918 
2919  case PT_FORMAT:
2920  num = 0;
2921 
2922  /* one overload */
2923 
2924  sig.arg1_type.is_generic = true;
2926 
2927  sig.arg2_type.is_generic = true;
2929 
2930  sig.arg3_type.is_generic = false;
2932 
2933  /* return type */
2934  sig.return_type.is_generic = false;
2936  def->overloads[num++] = sig;
2937 
2938  def->overloads_count = num;
2939  break;
2940 
2941  case PT_ROUND:
2942  num = 0;
2943  /* nine overloads */
2944  /* first overload for number: */
2945  sig.arg1_type.is_generic = true;
2947 
2948  sig.arg2_type.is_generic = true;
2950 
2951  /* return type */
2952  sig.return_type.is_generic = true;
2954 
2955  def->overloads[num++] = sig;
2956 
2957  /* overload for round('123', '1') */
2958  sig.arg1_type.is_generic = true;
2960 
2961  sig.arg2_type.is_generic = true;
2963 
2964  /* return type */
2965  sig.return_type.is_generic = true;
2967 
2968  def->overloads[num++] = sig;
2969 
2970  /* overload for round(date, 'year|month|day') */
2971  sig.arg1_type.is_generic = false;
2973 
2974  sig.arg2_type.is_generic = true;
2976 
2977  /* return type */
2978  sig.return_type.is_generic = false;
2980 
2981  def->overloads[num++] = sig;
2982 
2983  /* overload for round(datetime, 'year|month|day') */
2984  sig.arg1_type.is_generic = false;
2986 
2987  sig.arg2_type.is_generic = true;
2989 
2990  /* return type */
2991  sig.return_type.is_generic = false;
2993 
2994  def->overloads[num++] = sig;
2995 
2996  /* overload for round(timestamp, 'year|month|day') */
2997  sig.arg1_type.is_generic = false;
2999 
3000  sig.arg2_type.is_generic = true;
3002 
3003  /* return type */
3004  sig.return_type.is_generic = false;
3006 
3007  def->overloads[num++] = sig;
3008 
3009  /* overload for round(timestamptz, 'year|month|day') */
3010  sig.arg1_type.is_generic = false;
3012 
3013  sig.arg2_type.is_generic = true;
3015 
3016  /* return type */
3017  sig.return_type.is_generic = false;
3019 
3020  def->overloads[num++] = sig;
3021 
3022  /* overload for round(timestampltz, 'year|month|day') */
3023  sig.arg1_type.is_generic = false;
3025 
3026  sig.arg2_type.is_generic = true;
3028 
3029  /* return type */
3030  sig.return_type.is_generic = false;
3032 
3033  def->overloads[num++] = sig;
3034 
3035  /* overload for round(datetimetz, 'year|month|day') */
3036  sig.arg1_type.is_generic = false;
3038 
3039  sig.arg2_type.is_generic = true;
3041 
3042  /* return type */
3043  sig.return_type.is_generic = false;
3045 
3046  def->overloads[num++] = sig;
3047 
3048  /* overload for round(datetimeltz, 'year|month|day') */
3049  sig.arg1_type.is_generic = false;
3051 
3052  sig.arg2_type.is_generic = true;
3054 
3055  /* return type */
3056  sig.return_type.is_generic = false;
3058 
3059  def->overloads[num++] = sig;
3060 
3061  def->overloads_count = num;
3062  break;
3063 
3064  case PT_TRUNC:
3065  num = 0;
3066 
3067  /* nine overloads */
3068 
3069  /* number types */
3070  sig.arg1_type.is_generic = true;
3072 
3073  sig.arg2_type.is_generic = true;
3075 
3076  /* return type */
3077  sig.return_type.is_generic = true;
3079  def->overloads[num++] = sig;
3080 
3081  /* number types 2 */
3082  sig.arg1_type.is_generic = true;
3084 
3085  sig.arg2_type.is_generic = true;
3087 
3088  /* return type */
3089  sig.return_type.is_generic = true;
3091  def->overloads[num++] = sig;
3092 
3093  /* date */
3094  sig.arg1_type.is_generic = false;
3096 
3097  sig.arg2_type.is_generic = true;
3099 
3100  /* return type */
3101  sig.return_type.is_generic = false;
3103  def->overloads[num++] = sig;
3104 
3105  /* datetime */
3106  sig.arg1_type.is_generic = false;
3108 
3109  sig.arg2_type.is_generic = true;
3111 
3112  /* return type */
3113  sig.return_type.is_generic = false;
3115  def->overloads[num++] = sig;
3116 
3117  /* timestamp */
3118  sig.arg1_type.is_generic = false;
3120 
3121  sig.arg2_type.is_generic = true;
3123 
3124  /* return type */
3125  sig.return_type.is_generic = false;
3127  def->overloads[num++] = sig;
3128 
3129  /* datetimeltz */
3130  sig.arg1_type.is_generic = false;
3132 
3133  sig.arg2_type.is_generic = true;
3135 
3136  /* return type */
3137  sig.return_type.is_generic = false;
3139  def->overloads[num++] = sig;
3140 
3141  /* datetimetz */
3142  sig.arg1_type.is_generic = false;
3144 
3145  sig.arg2_type.is_generic = true;
3147 
3148  /* return type */
3149  sig.return_type.is_generic = false;
3151  def->overloads[num++] = sig;
3152 
3153  /* timestampltz */
3154  sig.arg1_type.is_generic = false;
3156 
3157  sig.arg2_type.is_generic = true;
3159 
3160  /* return type */
3161  sig.return_type.is_generic = false;
3163  def->overloads[num++] = sig;
3164 
3165  /* timestamptz */
3166  sig.arg1_type.is_generic = false;
3168 
3169  sig.arg2_type.is_generic = true;
3171 
3172  /* return type */
3173  sig.return_type.is_generic = false;
3175  def->overloads[num++] = sig;
3176 
3177  def->overloads_count = num;
3178  break;
3179 
3180  case PT_DEFAULTF:
3181  num = 0;
3182 
3183  /* one overload */
3184 
3185  sig.arg1_type.is_generic = true;
3187 
3188  /* return type */
3189  sig.return_type.is_generic = true;
3191  def->overloads[num++] = sig;
3192 
3193  def->overloads_count = num;
3194  break;
3195 
3196  case PT_INDEX_CARDINALITY:
3197  num = 0;
3198 
3199  /* one overload */
3200 
3201  sig.arg1_type.is_generic = true;
3203  sig.arg2_type.is_generic = true;
3205  sig.arg3_type.is_generic = false;
3207  sig.return_type.is_generic = false;
3209  def->overloads[num++] = sig;
3210 
3211  def->overloads_count = num;
3212  break;
3213 
3214  case PT_SIGN:
3215  num = 0;
3216 
3217  /* one overload */
3218 
3219  sig.arg1_type.is_generic = true;
3221 
3222  /* return type */
3223  sig.return_type.is_generic = false;
3225  def->overloads[num++] = sig;
3226 
3227  def->overloads_count = num;
3228  break;
3229 
3230  case PT_SETEQ:
3231  case PT_SETNEQ:
3232  case PT_SUBSET:
3233  case PT_SUBSETEQ:
3234  case PT_SUPERSET:
3235  case PT_SUPERSETEQ:
3236  num = 0;
3237 
3238  /* one overload */
3239 
3240  sig.arg1_type.is_generic = true;
3242 
3243  sig.arg2_type.is_generic = true;
3245 
3246  /* return type */
3247  sig.return_type.is_generic = false;
3249  def->overloads[num++] = sig;
3250 
3251  def->overloads_count = num;
3252  break;
3253 
3254  case PT_GE:
3255  case PT_GT:
3256  case PT_LT:
3257  case PT_LE:
3258  num = 0;
3259 
3260  /* two overloads */
3261 
3262  sig.arg1_type.is_generic = true;
3264 
3265  sig.arg2_type.is_generic = true;
3267 
3268  sig.return_type.is_generic = false;
3270  def->overloads[num++] = sig;
3271 
3272  sig.arg1_type.is_generic = true;
3274 
3275  sig.arg2_type.is_generic = true;
3277 
3278  sig.return_type.is_generic = false;
3280  def->overloads[num++] = sig;
3281 
3282  def->overloads_count = num;
3283  break;
3284 
3285  case PT_EQ:
3286  case PT_NE:
3287  case PT_NULLSAFE_EQ:
3288  num = 0;
3289 
3290  /* one overload */
3291 
3292  sig.arg1_type.is_generic = true;
3294 
3295  sig.arg2_type.is_generic = true;
3297 
3298  sig.return_type.is_generic = false;
3300  def->overloads[num++] = sig;
3301 
3302  def->overloads_count = num;
3303  break;
3304 
3305  case PT_NVL:
3306  case PT_IFNULL:
3307  case PT_COALESCE:
3308  num = 0;
3309 
3310  /* arg1 : generic string , arg2 : generic string */
3311  sig.arg1_type.is_generic = true;
3313  sig.arg2_type.is_generic = true;
3315  sig.return_type.is_generic = true;
3317  def->overloads[num++] = sig;
3318 
3319  /* arg1 : generic string , arg2 : generic any */
3320  sig.arg1_type.is_generic = true;
3322  sig.arg2_type.is_generic = true;
3324  sig.return_type.is_generic = false;
3326  def->overloads[num++] = sig;
3327 
3328  /* arg1 : generic bit , arg2 : generic bit */
3329  sig.arg1_type.is_generic = true;
3331  sig.arg2_type.is_generic = true;
3333  sig.return_type.is_generic = true;
3335  def->overloads[num++] = sig;
3336 
3337  /* arg1 : generic bit , arg2 : generic any */
3338  sig.arg1_type.is_generic = true;
3340  sig.arg2_type.is_generic = true;
3342  sig.return_type.is_generic = false;
3344  def->overloads[num++] = sig;
3345 
3346  /* arg1 : generic number , arg2 : generic number */
3347  sig.arg1_type.is_generic = true;
3349  sig.arg2_type.is_generic = true;
3351  sig.return_type.is_generic = true;
3353  def->overloads[num++] = sig;
3354 
3355  /* arg1 : generic number , arg2 : generic any */
3356  sig.arg1_type.is_generic = true;
3358  sig.arg2_type.is_generic = true;
3360  sig.return_type.is_generic = false;
3362  def->overloads[num++] = sig;
3363 
3364  /* arg1 : generic date , arg2 : generic date */
3365  sig.arg1_type.is_generic = true;
3367  sig.arg2_type.is_generic = true;
3369  sig.return_type.is_generic = true;
3371  def->overloads[num++] = sig;
3372 
3373  /* arg1 : generic date , arg2 : generic any */
3374  sig.arg1_type.is_generic = true;
3376  sig.arg2_type.is_generic = true;
3378  sig.return_type.is_generic = false;
3380  def->overloads[num++] = sig;
3381 
3382  /* arg1 : PT_TYPE_TIME , arg2 : PT_TYPE_TIME */
3383  sig.arg1_type.is_generic = false;
3385  sig.arg2_type.is_generic = false;
3387  sig.return_type.is_generic = false;
3389  def->overloads[num++] = sig;
3390 
3391  /* arg1 : PT_TYPE_TIME , arg2 : generic any */
3392  sig.arg1_type.is_generic = false;
3394  sig.arg2_type.is_generic = true;
3396  sig.return_type.is_generic = false;
3398  def->overloads[num++] = sig;
3399 
3400  /* arg1 : generic sequence, arg2 : generic sequence */
3401  sig.arg1_type.is_generic = true;
3403  sig.arg2_type.is_generic = true;
3405  sig.return_type.is_generic = true;
3407  def->overloads[num++] = sig;
3408 
3409  /* arg1 : generic sequence, arg2 : generic type any */
3410  sig.arg1_type.is_generic = true;
3412  sig.arg2_type.is_generic = true;
3414  sig.return_type.is_generic = false;
3416  def->overloads[num++] = sig;
3417 
3418  /* arg1 : generic lob, arg2 : generic lob */
3419  sig.arg1_type.is_generic = true;
3421  sig.arg2_type.is_generic = true;
3423  sig.return_type.is_generic = true;
3425  def->overloads[num++] = sig;
3426 
3427  /* arg1 : generic sequence, arg2 : generic type any */
3428  sig.arg1_type.is_generic = true;
3430  sig.arg2_type.is_generic = true;
3432  sig.return_type.is_generic = false;
3434  def->overloads[num++] = sig;
3435 
3436  /* arg1 : generic any, arg2 : generic any */
3437  sig.arg1_type.is_generic = true;
3439 
3440  sig.arg2_type.is_generic = true;
3442 
3443  sig.return_type.is_generic = true;
3445  def->overloads[num++] = sig;
3446 
3447  def->overloads_count = num;
3448 
3449  break;
3450 
3451  case PT_NULLIF:
3452  case PT_LEAST:
3453  case PT_GREATEST:
3454  num = 0;
3455 
3456  /* one overload */
3457 
3458  sig.arg1_type.is_generic = true;
3460 
3461  sig.arg2_type.is_generic = true;
3463 
3464  sig.return_type.is_generic = true;
3466  def->overloads[num++] = sig;
3467 
3468  def->overloads_count = num;
3469  break;
3470 
3471  case PT_NVL2:
3472  num = 0;
3473 
3474  /* arg1, arg1, arg3 : generic string */
3475  sig.arg1_type.is_generic = true;
3477  sig.arg2_type.is_generic = true;
3479  sig.arg3_type.is_generic = true;
3481  sig.return_type.is_generic = true;
3483  def->overloads[num++] = sig;
3484 
3485  /* arg1 : generic string , arg2, arg3 : generic any */
3486  sig.arg1_type.is_generic = true;
3488  sig.arg2_type.is_generic = true;
3490  sig.arg3_type.is_generic = true;
3492  sig.return_type.is_generic = false;
3494  def->overloads[num++] = sig;
3495 
3496  /* arg1, arg2, arg3 : generic bit */
3497  sig.arg1_type.is_generic = true;
3499  sig.arg2_type.is_generic = true;
3501  sig.arg3_type.is_generic = true;
3503  sig.return_type.is_generic = true;
3505  def->overloads[num++] = sig;
3506 
3507  /* arg1 : generic bit , arg2, arg3 : generic any */
3508  sig.arg1_type.is_generic = true;
3510  sig.arg2_type.is_generic = true;
3512  sig.arg3_type.is_generic = true;
3514  sig.return_type.is_generic = false;
3516  def->overloads[num++] = sig;
3517 
3518  /* arg1, arg2, arg3 : generic number */
3519  sig.arg1_type.is_generic = true;
3521  sig.arg2_type.is_generic = true;
3523  sig.arg3_type.is_generic = true;
3525  sig.return_type.is_generic = true;
3527  def->overloads[num++] = sig;
3528 
3529  /* arg1 : generic number , arg2, arg3 : generic any */
3530  sig.arg1_type.is_generic = true;
3532  sig.arg2_type.is_generic = true;
3534  sig.arg3_type.is_generic = true;
3536  sig.return_type.is_generic = false;
3538  def->overloads[num++] = sig;
3539 
3540  /* arg1, arg2, arg3 : generic date */
3541  sig.arg1_type.is_generic = true;
3543  sig.arg2_type.is_generic = true;
3545  sig.arg3_type.is_generic = true;
3547  sig.return_type.is_generic = true;
3549  def->overloads[num++] = sig;
3550 
3551  /* arg1 : generic date , arg2, arg3 : generic any */
3552  sig.arg1_type.is_generic = true;
3554  sig.arg2_type.is_generic = true;
3556  sig.arg3_type.is_generic = true;
3558  sig.return_type.is_generic = false;
3560  def->overloads[num++] = sig;
3561 
3562  /* arg1, arg2, arg3 : PT_TYPE_TIME */
3563  sig.arg1_type.is_generic = false;
3565  sig.arg2_type.is_generic = false;
3567  sig.arg3_type.is_generic = false;
3569  sig.return_type.is_generic = false;
3571  def->overloads[num++] = sig;
3572 
3573  /* arg1 : PT_TYPE_TIME , arg2, arg3 : generic any */
3574  sig.arg1_type.is_generic = false;
3576  sig.arg2_type.is_generic = true;
3578  sig.arg3_type.is_generic = true;
3580  sig.return_type.is_generic = false;
3582  def->overloads[num++] = sig;
3583 
3584  /* arg1, arg2, arg3 : generic sequence */
3585  sig.arg1_type.is_generic = true;
3587  sig.arg2_type.is_generic = true;
3589  sig.arg3_type.is_generic = true;
3591  sig.return_type.is_generic = true;
3593  def->overloads[num++] = sig;
3594 
3595  /* arg1 : generic sequence, arg2, arg3 : generic type any */
3596  sig.arg1_type.is_generic = true;
3598  sig.arg2_type.is_generic = true;
3600  sig.arg3_type.is_generic = true;
3602  sig.return_type.is_generic = false;
3604  def->overloads[num++] = sig;
3605 
3606  /* arg1, arg2, arg3 : generic lob */
3607  sig.arg1_type.is_generic = true;
3609  sig.arg2_type.is_generic = true;
3611  sig.arg3_type.is_generic = true;
3613  sig.return_type.is_generic = true;
3615  def->overloads[num++] = sig;
3616 
3617  /* arg1 : generic lob, arg2, arg3 : generic type any */
3618  sig.arg1_type.is_generic = true;
3620  sig.arg2_type.is_generic = true;
3622  sig.arg3_type.is_generic = true;
3624  sig.return_type.is_generic = false;
3626  def->overloads[num++] = sig;
3627 
3628  /* arg1, arg2, arg3 : generic any */
3629  sig.arg1_type.is_generic = true;
3631  sig.arg2_type.is_generic = true;
3633  sig.arg3_type.is_generic = true;
3635  sig.return_type.is_generic = true;
3637  def->overloads[num++] = sig;
3638 
3639  def->overloads_count = num;
3640 
3641  break;
3642  case PT_CONNECT_BY_ROOT:
3643  case PT_PRIOR:
3644  case PT_QPRIOR:
3645  num = 0;
3646 
3647  /* one overload */
3648 
3649  sig.arg1_type.is_generic = true;
3651 
3652  sig.return_type.is_generic = true;
3654  def->overloads[num++] = sig;
3655 
3656  def->overloads_count = num;
3657  break;
3658 
3659  case PT_UNARY_MINUS:
3660  num = 0;
3661 
3662  /* one overload */
3663 
3664  sig.arg1_type.is_generic = true;
3666 
3667  sig.return_type.is_generic = true;
3669  def->overloads[num++] = sig;
3670 
3671  def->overloads_count = num;
3672  break;
3673 
3674  case PT_TO_CHAR:
3675  num = 0;
3676 
3677  /* two overloads */
3678 
3679  sig.arg1_type.is_generic = true;
3681 
3682  sig.arg2_type.is_generic = true;
3684 
3685  sig.arg3_type.is_generic = false;
3687 
3688  sig.return_type.is_generic = false;
3690  def->overloads[num++] = sig;
3691 
3692  sig.arg1_type.is_generic = true;
3694 
3695  sig.arg2_type.is_generic = true;
3697 
3698  sig.arg3_type.is_generic = false;
3700 
3701  sig.return_type.is_generic = false;
3703  def->overloads[num++] = sig;
3704 
3705  def->overloads_count = num;
3706  break;
3707 
3708  case PT_ISNULL:
3709  case PT_IS_NULL:
3710  case PT_IS_NOT_NULL:
3711  num = 0;
3712 
3713  /* one overload */
3714 
3715  sig.arg1_type.is_generic = true;
3717 
3718  sig.return_type.is_generic = false;
3720  def->overloads[num++] = sig;
3721 
3722  def->overloads_count = num;
3723  break;
3724 
3725  case PT_IS:
3726  case PT_IS_NOT:
3727  num = 0;
3728 
3729  /* one overload */
3730 
3731  sig.arg1_type.is_generic = false;
3733 
3734  sig.arg2_type.is_generic = false;
3736 
3737  sig.return_type.is_generic = false;
3739  def->overloads[num++] = sig;
3740 
3741  def->overloads_count = num;
3742  break;
3743 
3744  case PT_SUBDATE:
3745  case PT_ADDDATE:
3746  num = 0;
3747 
3748  /* 8 overloads */
3749 
3750  sig.arg1_type.is_generic = true;
3752 
3753  sig.arg2_type.is_generic = false;
3755 
3756  sig.return_type.is_generic = false;
3758  def->overloads[num++] = sig;
3759 
3760  sig.arg1_type.is_generic = false;
3762 
3763  sig.arg2_type.is_generic = false;
3765 
3766  sig.return_type.is_generic = false;
3768  def->overloads[num++] = sig;
3769 
3770  sig.arg1_type.is_generic = false;
3772 
3773  sig.arg2_type.is_generic = false;
3775 
3776  sig.return_type.is_generic = false;
3778  def->overloads[num++] = sig;
3779 
3780  sig.arg1_type.is_generic = false;
3782 
3783  sig.arg2_type.is_generic = false;
3785 
3786  sig.return_type.is_generic = false;
3788  def->overloads[num++] = sig;
3789 
3790  sig.arg1_type.is_generic = false;
3792 
3793  sig.arg2_type.is_generic = false;
3795 
3796  sig.return_type.is_generic = false;
3798  def->overloads[num++] = sig;
3799 
3800  sig.arg1_type.is_generic = false;
3802 
3803  sig.arg2_type.is_generic = false;
3805 
3806  sig.return_type.is_generic = false;
3808  def->overloads[num++] = sig;
3809 
3810  sig.arg1_type.is_generic = false;
3812 
3813  sig.arg2_type.is_generic = false;
3815 
3816  sig.return_type.is_generic = false;
3818  def->overloads[num++] = sig;
3819 
3820  sig.arg1_type.is_generic = false;
3822 
3823  sig.arg2_type.is_generic = false;
3825 
3826  sig.return_type.is_generic = false;
3828  def->overloads[num++] = sig;
3829 
3830  def->overloads_count = num;
3831  break;
3832 
3833  case PT_GE_SOME:
3834  case PT_GT_SOME:
3835  case PT_LT_SOME:
3836  case PT_LE_SOME:
3837  case PT_GE_ALL:
3838  case PT_GT_ALL:
3839  case PT_LT_ALL:
3840  case PT_LE_ALL:
3841  num = 0;
3842 
3843  /* two overloads */
3844 
3845  sig.arg1_type.is_generic = true;
3847 
3848  sig.arg2_type.is_generic = false;
3849  sig.arg2_type.val.type = PT_TYPE_SET;
3850 
3851  sig.return_type.is_generic = false;
3853  def->overloads[num++] = sig;
3854 
3855  sig.arg1_type.is_generic = true;
3857 
3858  sig.arg2_type.is_generic = true;
3860 
3861  sig.return_type.is_generic = false;
3863  def->overloads[num++] = sig;
3864 
3865  def->overloads_count = num;
3866  break;
3867 
3868  case PT_EQ_SOME:
3869  case PT_NE_SOME:
3870  case PT_EQ_ALL:
3871  case PT_NE_ALL:
3872  case PT_IS_IN:
3873  case PT_IS_NOT_IN:
3874  num = 0;
3875 
3876  /* two overloads */
3877 
3878  sig.arg1_type.is_generic = true;
3880 
3881  sig.arg2_type.is_generic = false;
3882  sig.arg2_type.val.type = PT_TYPE_SET;
3883 
3884  sig.return_type.is_generic = false;
3886  def->overloads[num++] = sig;
3887 
3888  sig.arg1_type.is_generic = true;
3890 
3891  sig.arg2_type.is_generic = true;
3893 
3894  sig.return_type.is_generic = false;
3896  def->overloads[num++] = sig;
3897 
3898  def->overloads_count = num;
3899  break;
3900 
3901  case PT_BETWEEN_EQ_NA:
3902  case PT_BETWEEN_GE_INF:
3903  case PT_BETWEEN_GT_INF:
3904  case PT_BETWEEN_INF_LE:
3905  case PT_BETWEEN_INF_LT:
3906  case PT_LT_INF:
3907  case PT_GT_INF:
3908  /* these expressions are introduced during query rewriting and are used in the range operator. we should set the
3909  * return type to the type of the argument */
3910 
3911  num = 0;
3912 
3913  /* one overload */
3914 
3915  sig.arg1_type.is_generic = true;
3917 
3918  sig.return_type.is_generic = true;
3920  def->overloads[num++] = sig;
3921 
3922  def->overloads_count = num;
3923  break;
3924 
3925  case PT_UNIX_TIMESTAMP:
3926  num = 0;
3927 
3928  /* three overloads */
3929 
3930  /* UNIX_TIMESTAMP(string) */
3931  sig.arg1_type.is_generic = true;
3933  sig.return_type.is_generic = false;
3935  def->overloads[num++] = sig;
3936 
3937  /* UNIX_TIMESTAMP(date/time type) */
3938  sig.arg1_type.is_generic = true;
3940  sig.return_type.is_generic = false;
3942  def->overloads[num++] = sig;
3943 
3944  /* UNIX_TIMESTAMP (void) */
3945  sig.arg1_type.is_generic = false;
3947  sig.return_type.is_generic = false;
3949  def->overloads[num++] = sig;
3950 
3951  def->overloads_count = num;
3952  break;
3953 
3954  case PT_FROM_UNIXTIME:
3955  num = 0;
3956 
3957  /* two overloads */
3958 
3959  sig.arg1_type.is_generic = false;
3961 
3962  sig.arg2_type.is_generic = false;
3964 
3965  sig.arg3_type.is_generic = false;
3967 
3968  sig.return_type.is_generic = false;
3970  def->overloads[num++] = sig;
3971 
3972  sig.arg1_type.is_generic = false;
3974 
3975  sig.arg2_type.is_generic = true;
3977 
3978  sig.arg3_type.is_generic = false;
3980 
3981  sig.return_type.is_generic = false;
3983  def->overloads[num++] = sig;
3984 
3985  def->overloads_count = num;
3986  break;
3987 
3988  case PT_TIMESTAMP:
3989  num = 0;
3990 
3991  /* eight overloads */
3992 
3993  /* TIMESTAMP(STRING) */
3994  sig.arg1_type.is_generic = true;
3996  sig.return_type.is_generic = false;
3998  def->overloads[num++] = sig;
3999 
4000  /* TIMESTAMP(DATETIME) */
4001  sig.arg1_type.is_generic = true;
4003  sig.return_type.is_generic = false;
4005  def->overloads[num++] = sig;
4006 
4007  /* TIMESTAMP(STRING,STRING) */
4008  sig.arg1_type.is_generic = true;
4010  sig.arg2_type.is_generic = true;
4012  sig.return_type.is_generic = false;
4014  def->overloads[num++] = sig;
4015 
4016  /* TIMESTAMP(STRING,NUMBER) */
4017  sig.arg1_type.is_generic = true;
4019  sig.arg2_type.is_generic = true;
4021  sig.return_type.is_generic = false;
4023  def->overloads[num++] = sig;
4024 
4025 
4026  /* TIMESTAMP(STRING,TIME) */
4027  sig.arg1_type.is_generic = true;
4029  sig.arg2_type.is_generic = false;
4031  sig.return_type.is_generic = false;
4033  def->overloads[num++] = sig;
4034 
4035  /* TIMESTAMP(DATETIME,STRING) */
4036  sig.arg1_type.is_generic = true;
4038  sig.arg2_type.is_generic = true;
4040  sig.return_type.is_generic = false;
4042  def->overloads[num++] = sig;
4043 
4044  /* TIMESTAMP(DATETIME,TIME) */
4045  sig.arg1_type.is_generic = true;
4047  sig.arg2_type.is_generic = false;
4049  sig.return_type.is_generic = false;
4051  def->overloads[num++] = sig;
4052 
4053  /* TIMESTAMP(DATETIME,NUMBER) */
4054  sig.arg1_type.is_generic = true;
4056  sig.arg2_type.is_generic = true;
4058  sig.return_type.is_generic = false;
4060  def->overloads[num++] = sig;
4061 
4062  def->overloads_count = num;
4063  break;
4064 
4065  case PT_TYPEOF:
4066  num = 0;
4067 
4068  /* one overload */
4069 
4070  sig.arg1_type.is_generic = true;
4072  sig.return_type.is_generic = false;
4074  def->overloads[num++] = sig;
4075 
4076  def->overloads_count = num;
4077  break;
4078 
4079  case PT_EXTRACT:
4080  num = 0;
4081 
4082  /* five overloads */
4083 
4084  sig.arg1_type.is_generic = true;
4086  /* return type */
4087  sig.return_type.is_generic = false;
4089  def->overloads[num++] = sig;
4090 
4091  sig.arg1_type.is_generic = false;
4093  /* return type */
4094  sig.return_type.is_generic = false;
4096  def->overloads[num++] = sig;
4097 
4098  sig.arg1_type.is_generic = false;
4100  /* return type */
4101  sig.return_type.is_generic = false;
4103  def->overloads[num++] = sig;
4104 
4105  sig.arg1_type.is_generic = false;
4107  /* return type */
4108  sig.return_type.is_generic = false;
4110  def->overloads[num++] = sig;
4111 
4112  sig.arg1_type.is_generic = false;
4114  /* return type */
4115  sig.return_type.is_generic = false;
4117  def->overloads[num++] = sig;
4118 
4119  def->overloads_count = num;
4120  break;
4121 
4122  case PT_EVALUATE_VARIABLE:
4123  num = 0;
4124 
4125  /* one overload */
4126 
4127  sig.arg1_type.is_generic = false;
4129 
4130  sig.return_type.is_generic = false;
4132 
4133  def->overloads[num++] = sig;
4134 
4135  def->overloads_count = num;
4136  break;
4137 
4138  case PT_DEFINE_VARIABLE:
4139  num = 0;
4140 
4141  /* two overloads */
4142 
4143  sig.arg1_type.is_generic = false;
4145 
4146  sig.arg2_type.is_generic = true;
4148 
4149  sig.return_type.is_generic = false;
4151  def->overloads[num++] = sig;
4152 
4153  sig.arg1_type.is_generic = false;
4155 
4156  sig.arg2_type.is_generic = true;
4158 
4159  sig.return_type.is_generic = false;
4161  def->overloads[num++] = sig;
4162 
4163  def->overloads_count = num;
4164  break;
4165 
4166  case PT_EXEC_STATS:
4167  num = 0;
4168 
4169  /* one overload */
4170 
4171  sig.arg1_type.is_generic = true;
4173  sig.return_type.is_generic = false;
4175  def->overloads[num++] = sig;
4176 
4177  def->overloads_count = num;
4178  break;
4179 
4181  num = 0;
4182 
4183  /* one overload */
4184 
4185  /* arg1 */
4186  sig.arg1_type.is_generic = true;
4188 
4189  /* return type */
4190  sig.return_type.is_generic = false;
4192 
4193  def->overloads[num++] = sig;
4194 
4195  def->overloads_count = num;
4196  break;
4197 
4198  case PT_INET_ATON:
4199  num = 0;
4200 
4201  /* one overload */
4202 
4203  /* arg1 */
4204  sig.arg1_type.is_generic = true;
4206 
4207  /* return type */
4208  sig.return_type.is_generic = false;
4210 
4211  def->overloads[num++] = sig;
4212 
4213  def->overloads_count = num;
4214  break;
4215 
4216  case PT_INET_NTOA:
4217  num = 0;
4218 
4219  /* one overload */
4220 
4221  /* arg1 */
4222  sig.arg1_type.is_generic = false;
4224 
4225  /* return type */
4226  sig.return_type.is_generic = false;
4228 
4229  def->overloads[num++] = sig;
4230 
4231  def->overloads_count = num;
4232  break;
4233 
4234  case PT_COERCIBILITY:
4235  num = 0;
4236 
4237  /* one overload */
4238 
4239  /* arg1 */
4240  sig.arg1_type.is_generic = true;
4242 
4243  /* return type */
4244  sig.return_type.is_generic = false;
4246 
4247  def->overloads[num++] = sig;
4248 
4249  def->overloads_count = num;
4250  break;
4251 
4252  case PT_CHARSET:
4253  case PT_COLLATION:
4254  num = 0;
4255 
4256  /* one overload */
4257 
4258  /* arg1 */
4259  sig.arg1_type.is_generic = true;
4261 
4262  /* return type */
4263  sig.return_type.is_generic = false;
4265 
4266  def->overloads[num++] = sig;
4267 
4268  def->overloads_count = num;
4269  break;
4270 
4271  case PT_WIDTH_BUCKET:
4272  num = 0;
4273 
4274  /* 10 overloads */
4275 
4276  /* generic number */
4277  sig.arg1_type.is_generic = true;
4279 
4280  sig.arg2_type.is_generic = true;
4281  sig.arg2_type.val.generic_type = PT_GENERIC_TYPE_NUMBER; /* between */
4282 
4283  sig.arg3_type.is_generic = false;
4285 
4286  sig.return_type.is_generic = false;
4288 
4289  def->overloads[num++] = sig;
4290 
4291  /* generic string */
4292  sig.arg1_type.is_generic = true;
4294 
4295  sig.arg2_type.is_generic = true;
4296  sig.arg2_type.val.generic_type = PT_GENERIC_TYPE_STRING; /* between */
4297 
4298  sig.arg3_type.is_generic = false;
4300 
4301  sig.return_type.is_generic = false;
4303 
4304  def->overloads[num++] = sig;
4305 
4306  /* date */
4307  sig.arg1_type.is_generic = false;
4309 
4310  sig.arg2_type.is_generic = false;
4311  sig.arg2_type.val.type = PT_TYPE_DATE; /* between */
4312 
4313  sig.arg3_type.is_generic = false;
4315 
4316  sig.return_type.is_generic = false;
4318 
4319  def->overloads[num++] = sig;
4320 
4321  /* datetime */
4322  sig.arg1_type.is_generic = false;
4324 
4325  sig.arg2_type.is_generic = false;
4326  sig.arg2_type.val.type = PT_TYPE_DATETIME; /* between */
4327 
4328  sig.arg3_type.is_generic = false;
4330 
4331  sig.return_type.is_generic = false;
4333 
4334  def->overloads[num++] = sig;
4335 
4336  /* timestamp */
4337  sig.arg1_type.is_generic = false;
4339 
4340  sig.arg2_type.is_generic = false;
4341  sig.arg2_type.val.type = PT_TYPE_TIMESTAMP; /* between */
4342 
4343  sig.arg3_type.is_generic = false;
4345 
4346  sig.return_type.is_generic = false;
4348 
4349  def->overloads[num++] = sig;
4350 
4351  /* time */
4352  sig.arg1_type.is_generic = false;
4354 
4355  sig.arg2_type.is_generic = false;
4356  sig.arg2_type.val.type = PT_TYPE_TIME; /* between */
4357 
4358  sig.arg3_type.is_generic = false;
4360 
4361  sig.return_type.is_generic = false;
4363 
4364  def->overloads[num++] = sig;
4365 
4366  /* datetime with local timezone */
4367  sig.arg1_type.is_generic = false;
4369 
4370  sig.arg2_type.is_generic = false;
4371  sig.arg2_type.val.type = PT_TYPE_DATETIMELTZ; /* between */
4372 
4373  sig.arg3_type.is_generic = false;
4375 
4376  sig.return_type.is_generic = false;
4378 
4379  def->overloads[num++] = sig;
4380 
4381  /* datetime with timezone */
4382  sig.arg1_type.is_generic = false;
4384 
4385  sig.arg2_type.is_generic = false;
4386  sig.arg2_type.val.type = PT_TYPE_DATETIMETZ; /* between */
4387 
4388  sig.arg3_type.is_generic = false;
4390 
4391  sig.return_type.is_generic = false;
4393 
4394  def->overloads[num++] = sig;
4395 
4396  /* timestamp with local timezone */
4397  sig.arg1_type.is_generic = false;
4399 
4400  sig.arg2_type.is_generic = false;
4401  sig.arg2_type.val.type = PT_TYPE_TIMESTAMPLTZ; /* between */
4402 
4403  sig.arg3_type.is_generic = false;
4405 
4406  sig.return_type.is_generic = false;
4408 
4409  def->overloads[num++] = sig;
4410 
4411  /* timestamp with timezone */
4412  sig.arg1_type.is_generic = false;
4414 
4415  sig.arg2_type.is_generic = false;
4416  sig.arg2_type.val.type = PT_TYPE_TIMESTAMPTZ; /* between */
4417 
4418  sig.arg3_type.is_generic = false;
4420 
4421  sig.return_type.is_generic = false;
4423 
4424  def->overloads[num++] = sig;
4425 
4426  def->overloads_count = num;
4427  break;
4428 
4429  case PT_TRACE_STATS:
4430  num = 0;
4431 
4432  /* one overload */
4433 
4434  sig.return_type.is_generic = false;
4436  def->overloads[num++] = sig;
4437 
4438  def->overloads_count = num;
4439  break;
4440 
4441  case PT_INDEX_PREFIX:
4442  num = 0;
4443 
4444  /* three overloads */
4445 
4446  /* arg1 */
4447  sig.arg1_type.is_generic = true;
4449  /* arg2 */
4450  sig.arg2_type.is_generic = true;
4452  /* arg3 */
4453  sig.arg3_type.is_generic = true;
4455  /* return type */
4456  sig.return_type.is_generic = false;
4458  def->overloads[num++] = sig;
4459 
4460  /* arg1 */
4461  sig.arg1_type.is_generic = true;
4463  /* arg2 */
4464  sig.arg2_type.is_generic = true;
4466  /* arg3 */
4467  sig.arg3_type.is_generic = true;
4469  /* return type */
4470  sig.return_type.is_generic = false;
4472  def->overloads[num++] = sig;
4473 
4474  /* arg1 */
4475  sig.arg1_type.is_generic = true;
4477  /* arg2 */
4478  sig.arg2_type.is_generic = true;
4480  /* arg3 */
4481  sig.arg3_type.is_generic = true;
4483  /* return type */
4484  sig.return_type.is_generic = false;
4486  def->overloads[num++] = sig;
4487 
4488  def->overloads_count = num;
4489  break;
4490 
4491  case PT_SLEEP:
4492  num = 0;
4493 
4494  /* one overload */
4495 
4496  sig.arg1_type.is_generic = false;
4498 
4499  sig.return_type.is_generic = false;
4501  def->overloads[num++] = sig;
4502 
4503  def->overloads_count = num;
4504  break;
4505 
4506  case PT_DBTIMEZONE:
4507  case PT_SESSIONTIMEZONE:
4508  num = 0;
4509 
4510  /* one overload */
4511 
4512  /* return type */
4513  sig.return_type.is_generic = false;
4515  def->overloads[num++] = sig;
4516 
4517  def->overloads_count = num;
4518  break;
4519 
4520  case PT_TZ_OFFSET:
4521  num = 0;
4522 
4523  /* one overload */
4524 
4525  /* arg1 */
4526  sig.arg1_type.is_generic = false;
4528 
4529  /* return type */
4530  sig.return_type.is_generic = false;
4532  def->overloads[num++] = sig;
4533 
4534  def->overloads_count = num;
4535  break;
4536 
4537  case PT_NEW_TIME:
4538  num = 0;
4539  /* three overloads */
4540 
4541  /* arg1 */
4542  sig.arg1_type.is_generic = true;
4544 
4545  /* arg2 */
4546  sig.arg2_type.is_generic = false;
4548 
4549  /* arg3 */
4550  sig.arg3_type.is_generic = false;
4552 
4553  /* return type */
4554  sig.return_type.is_generic = true;
4556  def->overloads[num++] = sig;
4557 
4558  /* arg1 */
4559  sig.arg1_type.is_generic = false;
4561 
4562  /* arg2 */
4563  sig.arg2_type.is_generic = false;
4565 
4566  /* arg3 */
4567  sig.arg3_type.is_generic = false;
4569 
4570  /* return type */
4571  sig.return_type.is_generic = false;
4573  def->overloads[num++] = sig;
4574 
4575  /* arg1 */
4576  sig.arg1_type.is_generic = false;
4578 
4579  /* arg2 */
4580  sig.arg2_type.is_generic = false;
4582 
4583  /* arg3 */
4584  sig.arg3_type.is_generic = false;
4586 
4587  /* return type */
4588  sig.return_type.is_generic = false;
4590  def->overloads[num++] = sig;
4591 
4592  def->overloads_count = num;
4593  break;
4594 
4595  case PT_FROM_TZ:
4596  num = 0;
4597  /* two overloads */
4598 
4599  /* arg1 */
4600  sig.arg1_type.is_generic = true;
4602 
4603  /* arg2 */
4604  sig.arg2_type.is_generic = false;
4606 
4607  /* return type */
4608  sig.return_type.is_generic = true;
4610  def->overloads[num++] = sig;
4611 
4612  /* arg1 */
4613  sig.arg1_type.is_generic = false;
4615 
4616  /* arg2 */
4617  sig.arg2_type.is_generic = false;
4619 
4620  /* return type */
4621  sig.return_type.is_generic = false;
4623  def->overloads[num++] = sig;
4624 
4625  def->overloads_count = num;
4626  break;
4627 
4628  case PT_CONV_TZ:
4629  num = 0;
4630  /* four overloads */
4631 
4632  /* arg1 */
4633  sig.arg1_type.is_generic = false;
4635 
4636  /* return type */
4637  sig.return_type.is_generic = false;
4639  def->overloads[num++] = sig;
4640 
4641  /* arg1 */
4642  sig.arg1_type.is_generic = false;
4644 
4645  /* return type */
4646  sig.return_type.is_generic = false;
4648  def->overloads[num++] = sig;
4649 
4650  /* arg1 */
4651  sig.arg1_type.is_generic = false;
4653 
4654  /* return type */
4655  sig.return_type.is_generic = false;
4657  def->overloads[num++] = sig;
4658 
4659  /* arg1 */
4660  sig.arg1_type.is_generic = false;
4662 
4663  /* return type */
4664  sig.return_type.is_generic = false;
4666  def->overloads[num++] = sig;
4667 
4668  def->overloads_count = num;
4669  break;
4670 
4671  case PT_TO_DATETIME_TZ:
4672  num = 0;
4673 
4674  /* two overloads */
4675 
4676  /* arg1 */
4677  sig.arg1_type.is_generic = true;
4679  /* arg2 */
4680  sig.arg2_type.is_generic = true;
4682  /* arg3 */
4683  sig.arg3_type.is_generic = false;
4685 
4686  /* return type */
4687  sig.return_type.is_generic = false;
4689  def->overloads[num++] = sig;
4690 
4691  /* arg1 */
4692  sig.arg1_type.is_generic = true;
4694  /* arg2 */
4695  sig.arg2_type.is_generic = true;
4697  /* arg3 */
4698  sig.arg3_type.is_generic = false;
4700 
4701  /* return type */
4702  sig.return_type.is_generic = false;
4704  def->overloads[num++] = sig;
4705 
4706  def->overloads_count = num;
4707  break;
4708 
4709  case PT_TO_TIMESTAMP_TZ:
4710  num = 0;
4711 
4712  /* two overloads */
4713 
4714  /* arg1 */
4715  sig.arg1_type.is_generic = true;
4717  /* arg2 */
4718  sig.arg2_type.is_generic = true;
4720  /* arg3 */
4721  sig.arg3_type.is_generic = false;
4723 
4724  /* return type */
4725  sig.return_type.is_generic = false;
4727  def->overloads[num++] = sig;
4728 
4729  /* arg1 */
4730  sig.arg1_type.is_generic = true;
4732  /* arg2 */
4733  sig.arg2_type.is_generic = true;
4735  /* arg3 */
4736  sig.arg3_type.is_generic = false;
4738 
4739  /* return type */
4740  sig.return_type.is_generic = false;
4742  def->overloads[num++] = sig;
4743 
4744  def->overloads_count = num;
4745  break;
4746  case PT_CRC32:
4747  num = 0;
4748 
4749  /* one overload */
4750 
4751  /* arg1 */
4752  sig.arg1_type.is_generic = true;
4754 
4755  /* return type */
4756  sig.return_type.is_generic = false;
4758  def->overloads[num++] = sig;
4759 
4760  def->overloads_count = num;
4761  break;
4762  case PT_SCHEMA_DEF:
4763  num = 0;
4764 
4765  /* one overload */
4766 
4767  /* arg1 */
4768  sig.arg1_type.is_generic = true;
4770 
4771  /* return type */
4772  sig.return_type.is_generic = false;
4774  def->overloads[num++] = sig;
4775 
4776  def->overloads_count = num;
4777  break;
4778  case PT_JSON_CONTAINS:
4779  num = 0;
4780 
4781  /* two overloads */
4782 
4783  /* json_contains (target, candidate, path) */
4784  sig.arg1_type.is_generic = false;
4786 
4787  sig.arg2_type.is_generic = false;
4789 
4790  sig.arg3_type.is_generic = false;
4792 
4793  /* return type */
4794  sig.return_type.is_generic = false;
4796  def->overloads[num++] = sig;
4797 
4798  /* json_contains (target, candidate) */
4799  sig.arg1_type.is_generic = false;
4801 
4802  sig.arg2_type.is_generic = false;
4804 
4805  sig.arg3_type.is_generic = false;
4807 
4808  /* return type */
4809  sig.return_type.is_generic = false;
4811  def->overloads[num++] = sig;
4812 
4813  def->overloads_count = num;
4814  break;
4815  case PT_JSON_TYPE:
4816  num = 0;
4817 
4818  /* one overload */
4819 
4820  /* arg1 */
4821  sig.arg1_type.is_generic = false;
4823 
4824  /* return type */
4825  sig.return_type.is_generic = false;
4827  def->overloads[num++] = sig;
4828 
4829  def->overloads_count = num;
4830  break;
4831  case PT_JSON_VALID:
4832  num = 0;
4833 
4834  /* one overload */
4835 
4836  /* arg1 */
4837  sig.arg1_type.is_generic = false;
4839 
4840  /* return type */
4841  sig.return_type.is_generic = false;
4843  def->overloads[num++] = sig;
4844 
4845  def->overloads_count = num;
4846  break;
4847  case PT_JSON_DEPTH:
4848  num = 0;
4849 
4850  /* one overload */
4851 
4852  /* arg1 */
4853  sig.arg1_type.is_generic = false;
4855 
4856  /* return type */
4857  sig.return_type.is_generic = false;
4859  def->overloads[num++] = sig;
4860 
4861  def->overloads_count = num;
4862  break;
4863  case PT_JSON_LENGTH:
4864  num = 0;
4865 
4866  /* two overloads */
4867 
4868  /* arg1 */
4869  sig.arg1_type.is_generic = false;
4871 
4872  /* arg2 */
4873  sig.arg2_type.is_generic = false;
4875 
4876  /* return type */
4877  sig.return_type.is_generic = false;
4879  def->overloads[num++] = sig;
4880 
4881  /* arg1 */
4882  sig.arg1_type.is_generic = false;
4884 
4885  /* arg2 */
4886  sig.arg2_type.is_generic = false;
4888 
4889  /* return type */
4890  sig.return_type.is_generic = false;
4892  def->overloads[num++] = sig;
4893 
4894  def->overloads_count = num;
4895  break;
4896  case PT_JSON_EXTRACT:
4897  num = 0;
4898 
4899  /* one overload */
4900 
4901  /* arg1 */
4902  sig.arg1_type.is_generic = false;
4904 
4905  sig.arg2_type.is_generic = false;
4907 
4908  /* return type */
4909  sig.return_type.is_generic = false;
4911  def->overloads[num++] = sig;
4912 
4913  def->overloads_count = num;
4914  break;
4915  case PT_JSON_SEARCH:
4916  num = 0;
4917 
4918  /* arg1 */
4919  sig.arg1_type.is_generic = false;
4921  /* arg2 */
4922  sig.arg2_type.is_generic = false;
4924  /* arg3 */
4925  sig.arg3_type.is_generic = false;
4927 
4928  /* return type */
4929  sig.return_type.is_generic = false;
4931  def->overloads[num++] = sig;
4932 
4933  def->overloads_count = num;
4934  break;
4935  case PT_JSON_PRETTY:
4936  num = 0;
4937 
4938  /* one overload */
4939 
4940  /* arg1 */
4941  sig.arg1_type.is_generic = false;
4943 
4944  /* return type */
4945  sig.return_type.is_generic = false;
4947  def->overloads[num++] = sig;
4948 
4949  def->overloads_count = num;
4950  break;
4951  default:
4952  return false;
4953  }
4954 
4956 
4957  return true;
4958 }
4959 
4960 /*
4961  * pt_get_equivalent_type () - get the type to which a node should be
4962  * converted to in order to match an expression
4963  * definition
4964  * return : the new type
4965  * def_type(in) : the type defined in the expression signature
4966  * arg_type(in) : the type of the received expression argument
4967  */
4968 static PT_TYPE_ENUM
4969 pt_get_equivalent_type (const PT_ARG_TYPE def_type, const PT_TYPE_ENUM arg_type)
4970 {
4971  if (arg_type == PT_TYPE_NULL || arg_type == PT_TYPE_NONE)
4972  {
4973  /* either the argument is null or not defined */
4974  return arg_type;
4975  }
4976 
4977  if (!def_type.is_generic)
4978  {
4979  /* if the definition does not have a generic type, return the definition type */
4980  return def_type.val.type;
4981  }
4982 
4983  /* In some cases that involve ENUM (e.g. bit_length function) we need to convert ENUM to the other type even if the
4984  * types are equivalent */
4985  if (pt_are_equivalent_types (def_type, arg_type) && arg_type != PT_TYPE_ENUMERATION)
4986  {
4987  /* def_type includes type */
4988  if (arg_type == PT_TYPE_LOGICAL)
4989  {
4990  /* def_type is a generic type and even though logical type might be equivalent with the generic definition,
4991  * we are sure that we don't want it to be logical here */
4992  return PT_TYPE_INTEGER;
4993  }
4994  return arg_type;
4995  }
4996 
4997  /* At this point we do not have a clear match. We will return the "largest" type for the generic type defined in the
4998  * expression signature */
4999  switch (def_type.val.generic_type)
5000  {
5001  case PT_GENERIC_TYPE_ANY:
5002  if (arg_type == PT_TYPE_LOGICAL)
5003  {
5004  /* if PT_TYPE_LOGICAL apprears for a PT_GENERIC_TYPE_ANY, it should be converted to PT_TYPE_INTEGER. */
5005  return PT_TYPE_INTEGER;
5006  }
5007  return arg_type;
5008 
5010  if (PT_IS_PRIMITIVE_TYPE (arg_type))
5011  {
5012  return arg_type;
5013  }
5014  break;
5015 
5016  case PT_GENERIC_TYPE_LOB:
5017  if (PT_IS_LOB_TYPE (arg_type))
5018  {
5019  return arg_type;
5020  }
5021  break;
5022 
5024  return PT_TYPE_BIGINT;
5025 
5027  if (arg_type == PT_TYPE_ENUMERATION)
5028  {
5029  return PT_TYPE_SMALLINT;
5030  }
5031  return PT_TYPE_DOUBLE;
5032 
5033  case PT_GENERIC_TYPE_CHAR:
5036  return PT_TYPE_VARCHAR;
5037 
5038  case PT_GENERIC_TYPE_NCHAR:
5039  return PT_TYPE_VARNCHAR;
5040 
5041  case PT_GENERIC_TYPE_BIT:
5042  return PT_TYPE_VARBIT;
5043 
5044  case PT_GENERIC_TYPE_DATE:
5045  return PT_TYPE_DATETIME;
5046 
5047  default:
5048  return PT_TYPE_NONE;
5049  }
5050 
5051  return PT_TYPE_NONE;
5052 }
5053 
5054 /*
5055  * pt_coerce_expression_argument () - check if arg has the correct type and
5056  * wrap arg with a cast node to def_type
5057  * if needed.
5058  * return : NO_ERROR on success, ER_FAILED on error
5059  * parser(in) : the parser context
5060  * expr(in) : the expression to which arg belongs to
5061  * arg (in/out) : the expression argument that will be checked
5062  * def_type(in) : the type that was evaluated from the expression definition
5063  * data_type(in): precision and scale information for arg
5064  */
5065 static int
5066 pt_coerce_expression_argument (PARSER_CONTEXT * parser, PT_NODE * expr, PT_NODE ** arg, const PT_TYPE_ENUM def_type,
5067  PT_NODE * data_type)
5068 {
5069  PT_NODE *node = *arg;
5070  PT_NODE *new_node = NULL, *new_dt = NULL;
5071  TP_DOMAIN *d;
5073 
5074  if (node == NULL)
5075  {
5076  if (def_type != PT_TYPE_NONE)
5077  {
5078  return ER_FAILED;
5079  }
5080  return NO_ERROR;
5081  }
5082 
5083  if (node->type_enum == PT_TYPE_NULL)
5084  {
5085  /* no coercion needed for NULL arguments */
5086  return NO_ERROR;
5087  }
5088 
5089  if (def_type == node->type_enum)
5090  {
5091  return NO_ERROR;
5092  }
5093 
5094  if (def_type == PT_TYPE_LOGICAL)
5095  {
5096  /* no cast for type logical. this is an error and we should report it */
5097  return ER_FAILED;
5098  }
5099 
5100  /* set default scale and precision for parametrized types */
5101  switch (def_type)
5102  {
5103  case PT_TYPE_BIGINT:
5106  break;
5107 
5108  case PT_TYPE_NUMERIC:
5109  if (PT_IS_DISCRETE_NUMBER_TYPE (node->type_enum))
5110  {
5112  scale = 0;
5113  }
5114  else
5115  {
5118  }
5119  break;
5120 
5121  case PT_TYPE_VARCHAR:
5123  scale = 0;
5124  break;
5125 
5126  case PT_TYPE_VARNCHAR:
5128  scale = 0;
5129  break;
5130  case PT_TYPE_ENUMERATION:
5131  {
5132  /* Because enumerations should always be casted to a fully specified type, we only accept casting to an
5133  * enumeration type if we have a symmetrical expression (meaning that the arguments of the expression should
5134  * have the same type) and one of the arguments is already an enumeration. In this case, we will cast the
5135  * argument that is not an enumeration to the enumeration type of the other argument */
5136  PT_NODE *arg1, *arg2, *arg3;
5137  if (expr == NULL)
5138  {
5139  assert (false);
5140  return ER_FAILED;
5141  }
5142  if (!pt_is_symmetric_op (expr->info.expr.op))
5143  {
5144  assert (false);
5145  return ER_FAILED;
5146  }
5147 
5148  arg1 = expr->info.expr.arg1;
5149  arg2 = expr->info.expr.arg2;
5150  arg3 = expr->info.expr.arg3;
5151  /* we already know that arg is not an enumeration so we have to look for an enumeration between the other
5152  * arguments of expr */
5153  if (arg1 != NULL && arg1->type_enum == PT_TYPE_ENUMERATION)
5154  {
5155  new_dt = arg1->data_type;
5156  }
5157  else if (arg2 != NULL && arg2->type_enum == PT_TYPE_ENUMERATION)
5158  {
5159  new_dt = arg2->data_type;
5160  }
5161  else if (arg3 != NULL && arg3->type_enum == PT_TYPE_ENUMERATION)
5162  {
5163  new_dt = arg3->data_type;
5164  }
5165  if (new_dt == NULL)
5166  {
5167  assert (false);
5170  return ER_FAILED;
5171  }
5172  break;
5173  }
5174  default:
5177  break;
5178  }
5179 
5180  if (node->type_enum == PT_TYPE_MAYBE)
5181  {
5182  if ((node->node_type == PT_EXPR && pt_is_op_hv_late_bind (node->info.expr.op))
5183  || (node->node_type == PT_SELECT && node->info.query.is_subquery == PT_IS_SUBQUERY))
5184  {
5185  /* wrap with cast, instead of setting expected domain */
5186  new_node = pt_wrap_with_cast_op (parser, node, def_type, precision, scale, new_dt);
5187 
5188  if (new_node == NULL)
5189  {
5190  return ER_FAILED;
5191  }
5192  /* reset expected domain of wrapped argument to NULL: it will be replaced at XASL generation with
5193  * DB_TYPE_VARIABLE domain */
5194  node->expected_domain = NULL;
5195 
5196  *arg = new_node;
5197  }
5198  else
5199  {
5200  if (new_dt != NULL)
5201  {
5202  d = pt_data_type_to_db_domain (parser, new_dt, NULL);
5203  }
5204  else
5205  {
5206  d =
5209  }
5210  if (d == NULL)
5211  {
5212  return ER_FAILED;
5213  }
5214  /* make sure the returned domain is cached */
5215  d = tp_domain_cache (d);
5217  }
5218 
5219  if (node->node_type == PT_HOST_VAR)
5220  {
5222  }
5223  }
5224  else
5225  {
5226  if (PT_IS_COLLECTION_TYPE (def_type))
5227  {
5228  if (data_type == NULL)
5229  {
5230  if (PT_IS_COLLECTION_TYPE (node->type_enum))
5231  {
5232  data_type = parser_copy_tree_list (parser, node->data_type);
5233  }
5234  else
5235  {
5236  /* this is might not be an error and we might do more damage if we cast it */
5237  return NO_ERROR;
5238  }
5239  }
5240  new_node = pt_wrap_collection_with_cast_op (parser, node, def_type, data_type, false);
5241  }
5242  else
5243  {
5244  new_node = pt_wrap_with_cast_op (parser, node, def_type, precision, scale, new_dt);
5245  }
5246  if (new_node == NULL)
5247  {
5248  return ER_FAILED;
5249  }
5251  {
5252  assert (new_node->node_type == PT_EXPR);
5254  }
5255 
5256  *arg = new_node;
5257  }
5258 
5259  return NO_ERROR;
5260 }
5261 
5262 /*
5263  * pt_are_unmatchable_types () - check if the two types cannot be matched
5264  * return : true if the two types cannot be matched
5265  * def_type(in) : an expression definition type
5266  * op_type(in) : an argument type
5267  */
5268 static bool
5269 pt_are_unmatchable_types (const PT_ARG_TYPE def_type, const PT_TYPE_ENUM op_type)
5270 {
5271  /* PT_TYPE_NONE does not match anything */
5272  if (op_type == PT_TYPE_NONE && !(def_type.is_generic == false && def_type.val.type == PT_TYPE_NONE))
5273  {
5274  return true;
5275  }
5276 
5277  if (op_type != PT_TYPE_NONE && def_type.is_generic == false && def_type.val.type == PT_TYPE_NONE)
5278  {
5279  return true;
5280  }
5281 
5282  return false;
5283 }
5284 
5285 /*
5286  * pt_are_equivalent_types () - check if a node type is equivalent with a
5287  * definition type
5288  * return : true if the types are equivalent, false otherwise
5289  * def_type(in) : the definition type
5290  * op_type(in) : argument type
5291  */
5292 static bool
5293 pt_are_equivalent_types (const PT_ARG_TYPE def_type, const PT_TYPE_ENUM op_type)
5294 {
5295  if (!def_type.is_generic)
5296  {
5297  if (def_type.val.type == op_type && op_type == PT_TYPE_NONE)
5298  {
5299  /* return false if both arguments are of type none */
5300  return false;
5301  }
5302  if (def_type.val.type == op_type)
5303  {
5304  /* return true if both have the same type */
5305  return true;
5306  }
5307  /* if def_type is a PT_TYPE_ENUM and the conditions above did not hold then the two types are not equivalent. */
5308  return false;
5309  }
5310 
5311  switch (def_type.val.generic_type)
5312  {
5313  case PT_GENERIC_TYPE_ANY:
5314  /* PT_GENERIC_TYPE_ANY is equivalent to any type */
5315  return true;
5317  if (PT_IS_PRIMITIVE_TYPE (op_type))
5318  {
5319  return true;
5320  }
5321  break;
5323  if (PT_IS_DISCRETE_NUMBER_TYPE (op_type) || op_type == PT_TYPE_ENUMERATION)
5324  {
5325  /* PT_GENERIC_TYPE_DISCRETE_NUMBER is equivalent with SHORT, INTEGER and BIGINT */
5326  return true;
5327  }
5328  break;
5329 
5331  if (PT_IS_NUMERIC_TYPE (op_type) || op_type == PT_TYPE_ENUMERATION)
5332  {
5333  /* any NUMBER type is equivalent with PT_GENERIC_TYPE_NUMBER */
5334  return true;
5335  }
5336  break;
5337 
5339  if (PT_IS_CHAR_STRING_TYPE (op_type) || op_type == PT_TYPE_ENUMERATION)
5340  {
5341  /* any STRING type is equivalent with PT_GENERIC_TYPE_STRING */
5342  return true;
5343  }
5344  break;
5345 
5346  case PT_GENERIC_TYPE_CHAR:
5347  if (op_type == PT_TYPE_CHAR || op_type == PT_TYPE_VARCHAR || op_type == PT_TYPE_ENUMERATION)
5348  {
5349  /* CHAR and VARCHAR are equivalent to PT_GENERIC_TYPE_CHAR */
5350  return true;
5351  }
5352  break;
5353 
5354  case PT_GENERIC_TYPE_NCHAR:
5355  if (op_type == PT_TYPE_NCHAR || op_type == PT_TYPE_VARNCHAR)
5356  {
5357  /* NCHAR and VARNCHAR are equivalent to PT_GENERIC_TYPE_NCHAR */
5358  return true;
5359  }
5360  break;
5361 
5362  case PT_GENERIC_TYPE_BIT:
5363  if (PT_IS_BIT_STRING_TYPE (op_type))
5364  {
5365  /* BIT and BIT VARYING are equivalent to PT_GENERIC_TYPE_BIT */
5366  return true;
5367  }
5368  break;
5369 
5371  if (PT_IS_DATE_TIME_TYPE (op_type))
5372  {
5373  return true;
5374  }
5375  break;
5376  case PT_GENERIC_TYPE_DATE:
5377  if (PT_HAS_DATE_PART (op_type))
5378  {
5379  return true;
5380  }
5381  break;
5383  if (PT_IS_COLLECTION_TYPE (op_type))
5384  {
5385  /* any COLLECTION is equivalent with PT_GENERIC_TYPE_SEQUENCE */
5386  return true;
5387  }
5388  break;
5389 
5390  default:
5391  return false;
5392  }
5393 
5394  return false;
5395 }
5396 
5397 static PT_NODE *
5398 pt_evaluate_new_data_type (const PT_TYPE_ENUM old_type, const PT_TYPE_ENUM new_type, PT_NODE * data_type)
5399 {
5400  if (new_type == PT_TYPE_NUMERIC)
5401  {
5402  switch (old_type)
5403  {
5404  case PT_TYPE_SMALLINT:
5405  case PT_TYPE_INTEGER:
5406  case PT_TYPE_BIGINT:
5407  return data_type;
5408  default:
5409  /* any other type should set maximum scale and precision since we cannot correctly evaluate them during type
5410  * checking */
5411  return NULL;
5412  }
5413  }
5414  return NULL;
5415 }
5416 
5417 
5418 /*
5419  * pt_infer_common_type - get the common type of the three arguments
5420  *
5421  * return : the common type
5422  * op(in) : expression identifier
5423  * arg1(in/out) : type of the first argument
5424  * arg2(in/out) : type of the second argument
5425  * arg3(in/out) : type of the third argument
5426  *
5427  * Notes: Unlike pt_common_type_op, this function infers a type for host
5428  * variables also. We can do this here because this function is called
5429  * after the expression signature has been applied. This means that
5430  * a type is left as PT_TYPE_MAYBE because the expression is symmetric
5431  * and the signature defines PT_GENERIC_TYPE_ANY or
5432  * PT_GENERIC_TYPE_PRIMITIVE for this argument.
5433  * There are some issues with collection types that this function does
5434  * not address. Collections are composed types so it's not enough to
5435  * decide that the common type is a collection type. We should also
5436  * infer the common type of the collection elements. This should be
5437  * done in the calling function because we have access to the actual
5438  * arguments there.
5439  */
5440 static PT_TYPE_ENUM
5441 pt_infer_common_type (const PT_OP_TYPE op, PT_TYPE_ENUM * arg1, PT_TYPE_ENUM * arg2, PT_TYPE_ENUM * arg3,
5442  const TP_DOMAIN * expected_domain)
5443 {
5444  PT_TYPE_ENUM common_type = PT_TYPE_NONE;
5445  PT_TYPE_ENUM arg1_eq_type = *arg1;
5446  PT_TYPE_ENUM arg2_eq_type = *arg2;
5447  PT_TYPE_ENUM arg3_eq_type = *arg3;
5448  PT_TYPE_ENUM expected_type = PT_TYPE_NONE;
5449  assert (pt_is_symmetric_op (op));
5450 
5451  /* We ignore PT_TYPE_NONE arguments because, in the context of this function, if an argument is of type PT_TYPE_NONE
5452  * then it is not defined in the signature that we have decided to use. */
5453 
5454  if (expected_domain != NULL)
5455  {
5456  expected_type = pt_db_to_type_enum (TP_DOMAIN_TYPE (expected_domain));
5457  }
5458 
5459  common_type = arg1_eq_type;
5460  if (arg1_eq_type != PT_TYPE_NONE)
5461  {
5462  /* arg1 is defined */
5463  if (arg2_eq_type != PT_TYPE_NONE)
5464  {
5465  /* arg2 is defined */
5466  common_type = pt_common_type_op (arg1_eq_type, op, arg2_eq_type);
5467  if (common_type == PT_TYPE_MAYBE && op != PT_COALESCE)
5468  {
5469  /* "mirror" the known argument type to the other argument if the later is PT_TYPE_MAYBE */
5470  if (!pt_is_op_hv_late_bind (op))
5471  {
5472  if (arg1_eq_type != PT_TYPE_MAYBE)
5473  {
5474  /* then arg2_eq_type is PT_TYPE_MAYBE */
5475  arg2_eq_type = arg1_eq_type;
5476  common_type = arg1_eq_type;
5477  }
5478  else if (arg2_eq_type != PT_TYPE_MAYBE)
5479  {
5480  arg1_eq_type = arg2_eq_type;
5481  common_type = arg2_eq_type;
5482  }
5483  }
5484  }
5485  }
5486  }
5487  if (arg3_eq_type != PT_TYPE_NONE)
5488  {
5489  /* at this point either all arg1_eq_type, arg2_eq_type and common_type are PT_TYPE_MAYBE, or are already set to a
5490  * PT_TYPE_ENUM */
5491  common_type = pt_common_type_op (common_type, op, arg3_eq_type);
5492  if (common_type == PT_TYPE_MAYBE)
5493  {
5494  /* either arg3_eq_type is PT_TYPE_MABYE or arg1, arg2 and common_type were PT_TYPE_MABYE */
5495  if (arg3_eq_type == PT_TYPE_MAYBE)
5496  {
5497  if (arg1_eq_type != PT_TYPE_MAYBE)
5498  {
5499  common_type = arg1_eq_type;
5500  arg3_eq_type = common_type;
5501  }
5502  }
5503  else
5504  {
5505  arg1_eq_type = arg3_eq_type;
5506  arg2_eq_type = arg3_eq_type;
5507  common_type = arg3_eq_type;
5508  }
5509  }
5510  }
5511  if (common_type == PT_TYPE_MAYBE && expected_type != PT_TYPE_NONE && !pt_is_op_hv_late_bind (op))
5512  {
5513  /* if expected type if not PT_TYPE_NONE then a expression higher up in the parser tree has set an expected domain
5514  * for this node and we can use it to set the expected domain of the arguments */
5515  common_type = expected_type;
5516  }
5517 
5518  /* final check : common_type should be PT_TYPE_MAYBE at this stage only for a small number of operators (PLUS,
5519  * MINUS,..) and only when at least one of the arguments is PT_TYPE_MAYBE; -when common type is PT_TYPE_MAYBE, then
5520  * leave all arguments as they are: either TYPE_MAYBE, either a concrete TYPE - without cast. The operator's
5521  * arguments will be resolved at execution in this case -if common type is a concrete type, then force all other
5522  * arguments to the common type (this should apply for most operators) */
5523  if (common_type != PT_TYPE_MAYBE)
5524  {
5525  *arg1 = (arg1_eq_type == PT_TYPE_NONE) ? PT_TYPE_NONE : common_type;
5526  *arg2 = (arg2_eq_type == PT_TYPE_NONE) ? PT_TYPE_NONE : common_type;
5527  *arg3 = (arg3_eq_type == PT_TYPE_NONE) ? PT_TYPE_NONE : common_type;
5528  }
5529  return common_type;
5530 }
5531 
5532 /*
5533  * pt_get_common_collection_type - get the common type of the elements in a
5534  * collection
5535  * return : the common type
5536  * set(in) : the collection
5537  * is_multitype : specifies that there are at least two different types in
5538  * collection's elements.
5539  *
5540  * Note: The PT_TYPE_MAYBE is ignored and all numeric types
5541  * are counted as one.
5542  */
5543 static PT_TYPE_ENUM
5544 pt_get_common_collection_type (const PT_NODE * set, bool * is_multitype)
5545 {
5546  PT_TYPE_ENUM common_type = PT_TYPE_NONE, temp_type = PT_TYPE_NONE;
5547  bool is_multitype_temp = false;
5548  PT_NODE *data_type = NULL;
5549  assert (set != NULL);
5550  assert (PT_IS_COLLECTION_TYPE (set->type_enum));
5551 
5552  if (set->node_type == PT_EXPR && set->info.expr.op == PT_CAST)
5553  {
5554  /* cast nodes keep element information in the cast_type node */
5555  data_type = set->info.expr.cast_type;
5556  }
5557  else
5558  {
5559  data_type = set->data_type;
5560  }
5561 
5562  if (data_type)
5563  {
5564  common_type = data_type->type_enum;
5565  for (data_type = data_type->next; data_type; data_type = data_type->next)
5566  {
5567  temp_type = data_type->type_enum;
5568  if (common_type != temp_type && temp_type != PT_TYPE_MAYBE
5569  && (!PT_IS_NUMERIC_TYPE (temp_type) || !PT_IS_NUMERIC_TYPE (common_type)))
5570  {
5571  is_multitype_temp = true;
5572  }
5573  common_type = pt_common_type (common_type, temp_type);
5574  }
5575  }
5576 
5577  if (is_multitype)
5578  {
5579  *is_multitype = is_multitype_temp;
5580  }
5581 
5582  return common_type;
5583 }
5584 
5585 /*
5586  * pt_is_collection_of_type - check if a node is of type
5587  * collection_type of element_type
5588  * return : true if the node is of type collection of element_type
5589  * node(in) : the node to be checked
5590  * collection_type(in) : the type of the collection (SET, MULTISET, etc)
5591  * element_type(in) : the type of the elements in the collection
5592  */
5593 static bool
5594 pt_is_collection_of_type (const PT_NODE * node, const PT_TYPE_ENUM collection_type, const PT_TYPE_ENUM element_type)
5595 {
5596  PT_NODE *temp = NULL;
5597  assert (node != NULL);
5598 
5599  if (!PT_IS_COLLECTION_TYPE (node->type_enum))
5600  {
5601  /* if it's not a collection return false */
5602  return false;
5603  }
5604 
5605  if (node->type_enum != collection_type)
5606  {
5607  /* if it's not the same type of collection return false */
5608  return false;
5609  }
5610 
5611  for (temp = node->data_type; temp; temp = temp->next)
5612  {
5613  if (element_type != temp->type_enum)
5614  {
5615  /* if collection has an element of different type than element_type return false */
5616  return false;
5617  }
5618  }
5619 
5620  return true;
5621 }
5622 
5623 /*
5624  * pt_coerce_range_expr_arguments - apply signature sig to the arguments of the
5625  * logical expression expr
5626  * return : the (possibly modified) expr or NULL on error
5627  * parser(in) : the parser context
5628  * expr(in) : the SQL expression
5629  * arg1(in) : first argument of the expression
5630  * arg2(in) : second argument of the expression
5631  * arg3(in) : third argument of the expression
5632  * sig(in) : the expression signature
5633  *
5634  */
5635 static PT_NODE *
5636 pt_coerce_range_expr_arguments (PARSER_CONTEXT * parser, PT_NODE * expr, PT_NODE * arg1, PT_NODE * arg2, PT_NODE * arg3,
5638 {
5639  PT_TYPE_ENUM arg1_type = PT_TYPE_NONE, arg2_type = PT_TYPE_NONE;
5640  PT_TYPE_ENUM arg3_type = PT_TYPE_NONE;
5641  PT_TYPE_ENUM arg1_eq_type = PT_TYPE_NONE, arg2_eq_type = PT_TYPE_NONE;
5642  PT_TYPE_ENUM arg3_eq_type = PT_TYPE_NONE;
5643  PT_TYPE_ENUM common_type = PT_TYPE_NONE;
5644  PT_OP_TYPE op;
5645  int error = NO_ERROR;
5646 
5647  op = expr->info.expr.op;
5648 
5649  arg1 = expr->info.expr.arg1;
5650  if (arg1)
5651  {
5652  arg1_type = arg1->type_enum;
5653  }
5654 
5655  arg2 = expr->info.expr.arg2;
5656  if (arg2)
5657  {
5658  arg2_type = arg2->type_enum;
5659  }
5660 
5661  arg3 = expr->info.expr.arg3;
5662  if (arg3)
5663  {
5664  arg3_type = arg3->type_enum;
5665  }
5666 
5667  arg1_eq_type = pt_get_equivalent_type (sig.arg1_type, arg1_type);
5668  arg2_eq_type = pt_get_equivalent_type (sig.arg2_type, arg2_type);
5669  arg3_eq_type = pt_get_equivalent_type (sig.arg3_type, arg3_type);
5670 
5671  /* for range expressions the second argument may be a collection or a query. */
5672  if (PT_IS_QUERY_NODE_TYPE (arg2->node_type))
5673  {
5674  /* the select list must have only one element and the first argument has to be of the same type as the argument
5675  * from the select list */
5676  PT_NODE *arg2_list = NULL;
5677 
5678  /* duplicates are not relevant; order by is not relevant; */
5681 
5682  arg2_list = pt_get_select_list (parser, arg2);
5683  if (PT_IS_COLLECTION_TYPE (arg2_list->type_enum) && arg2_list->node_type == PT_FUNCTION)
5684  {
5685  expr->type_enum = PT_TYPE_LOGICAL;
5686  return expr;
5687  }
5688  if (pt_length_of_select_list (arg2_list, EXCLUDE_HIDDEN_COLUMNS) != 1)
5689  {
5691  return NULL;
5692  }
5693  arg2_type = arg2_list->type_enum;
5694  if (pt_is_enumeration_special_comparison (arg1, op, arg2))
5695  {
5696  /* In case of 'ENUM IN [query]' we need to convert all elements of right argument to the ENUM type in order
5697  * to preserve an eventual index scan on left argument */
5698  common_type = PT_TYPE_ENUMERATION;
5699  }
5700  else
5701  {
5702  common_type = pt_common_type_op (arg1_eq_type, op, arg2_type);
5703  }
5704  if (PT_IS_COLLECTION_TYPE (common_type))
5705  {
5706  /* we cannot make a decision during type checking in this case */
5707  return expr;
5708  }
5709  if ((PT_IS_NUMERIC_TYPE (arg1_type) && PT_IS_NUMERIC_TYPE (common_type)) || common_type == PT_TYPE_MAYBE)
5710  {
5711  /* do not cast between numeric types */
5712  arg1_eq_type = arg1_type;
5713  }
5714  else
5715  {
5716  arg1_eq_type = common_type;
5717  }
5718  if ((PT_IS_NUMERIC_TYPE (arg2_type) && PT_IS_NUMERIC_TYPE (common_type)) || common_type == PT_TYPE_MAYBE)
5719  {
5720  arg2_eq_type = arg2_type;
5721  }
5722  else
5723  {
5724  arg2_eq_type = common_type;
5725  }
5726 
5727  error = pt_coerce_expression_argument (parser, expr, &arg1, arg1_eq_type, NULL);
5728  if (error != NO_ERROR)
5729  {
5730  return NULL;
5731  }
5732  else
5733  {
5734  expr->info.expr.arg1 = arg1;
5735  }
5736 
5737  if (pt_wrap_select_list_with_cast_op (parser, arg2, arg2_eq_type, 0, 0, NULL, false) != NO_ERROR)
5738  {
5739  return NULL;
5740  }
5741  }
5742  else if (PT_IS_COLLECTION_TYPE (arg2_type))
5743  {
5744  /* Because we're using collections, semantically, all three cases below are valid: 1. SELECT * FROM tbl WHERE
5745  * int_col in {integer, set, object, date} 2. SELECT * FROM tbl WHERE int_col in {str, str, str} 3. SELECT * FROM
5746  * tbl WHERE int_col in {integer, integer, integer} We will only coerce arg2 if there is a common type between
5747  * arg1 and all elements from the collection arg2. We do not consider the case in which we cannot discern a
5748  * common type to be a semantic error and we rely on the functionality of the comparison operators to be applied
5749  * correctly on the expression during expression evaluation. This means that we consider that the user knew what
5750  * he was doing in the first case but wanted to write something else in the second case. */
5751  PT_TYPE_ENUM collection_type = PT_TYPE_NONE;
5752  bool should_cast = false;
5753  PT_NODE *data_type = NULL;
5754 
5755  if (pt_is_enumeration_special_comparison (arg1, op, arg2))
5756  {
5757  /* In case of 'ENUM IN (...)' we need to convert all elements of right argument to the ENUM type in order to
5758  * preserve an eventual index scan on left argument */
5759  common_type = arg1_eq_type = collection_type = PT_TYPE_ENUMERATION;
5760  }
5761  else
5762  {
5763  common_type = collection_type = pt_get_common_collection_type (arg2, &should_cast);
5764  if (common_type != PT_TYPE_NONE)
5765  {
5766  common_type = pt_common_type_op (arg1_eq_type, op, common_type);
5767  if (common_type != PT_TYPE_NONE)
5768  {
5769  arg1_eq_type = common_type;
5770  }
5771  }
5772  }
5773  if (common_type == PT_TYPE_OBJECT || PT_IS_COLLECTION_TYPE (common_type))
5774  {
5775  /* we cannot make a cast decision here. to keep backwards compatibility we will call pt_coerce_value which
5776  * will only work on constants */
5777  PT_NODE *temp = NULL, *msg_temp = NULL;
5778  msg_temp = parser->error_msgs;
5779  (void) pt_coerce_value (parser, arg2, arg2, PT_TYPE_SET, arg2->data_type);
5780  if (pt_has_error (parser))
5781  {
5782  /* ignore errors */
5783  parser_free_tree (parser, parser->error_msgs);
5784  parser->error_msgs = msg_temp;
5785  }
5786  if (pt_coerce_value (parser, arg1, arg1, common_type, NULL) != NO_ERROR)
5787  {
5788  expr->type_enum = PT_TYPE_NONE;
5789  }
5790  if (arg2->node_type == PT_VALUE)
5791  {
5792  for (temp = arg2->info.value.data_value.set; temp; temp = temp->next)
5793  {
5794  if (common_type != temp->type_enum)
5795  {
5796  msg_temp = parser->error_msgs;
5797  parser->error_msgs = NULL;
5798  (void) pt_coerce_value (parser, temp, temp, common_type, NULL);
5799  if (pt_has_error (parser))
5800  {
5801  parser_free_tree (parser, parser->error_msgs);
5802  }
5803  parser->error_msgs = msg_temp;
5804  }
5805  }
5806  }
5807 
5808  return expr;
5809  }
5810 
5811  if (PT_IS_NUMERIC_TYPE (arg1_type) && PT_IS_NUMERIC_TYPE (arg1_eq_type))
5812  {
5813  /* do not cast between numeric types */
5814  arg1_eq_type = arg1_type;
5815  }
5816  else
5817  {
5818  error = pt_coerce_expression_argument (parser, expr, &arg1, arg1_eq_type, NULL);
5819  if (arg1 == NULL)
5820  {
5821  return NULL;
5822  }
5823  expr->info.expr.arg1 = arg1;
5824  }
5825 
5826  /* verify if we should cast arg2 to appropriate type */
5827  if (common_type == PT_TYPE_NONE /* check if there is a valid common type between members of arg2 and arg1. */
5828  || (!should_cast /* check if there are at least two different types in arg2 */
5829  && (collection_type == common_type
5830  || (PT_IS_NUMERIC_TYPE (collection_type) && PT_IS_NUMERIC_TYPE (common_type))
5831  || (PT_IS_CHAR_STRING_TYPE (collection_type) && PT_IS_CHAR_STRING_TYPE (common_type)))))
5832  {
5833  return expr;
5834  }
5835 
5836  /* we can perform an implicit cast here */
5837  data_type = parser_new_node (parser, PT_DATA_TYPE);
5838  if (data_type == NULL)
5839  {
5840  return NULL;
5841  }
5842  data_type->info.data_type.dec_precision = 0;
5843  data_type->info.data_type.precision = 0;
5844  data_type->type_enum = common_type;
5845  if (PT_IS_PARAMETERIZED_TYPE (common_type))
5846  {
5847  PT_NODE *temp = NULL;
5848  int precision = 0, scale = 0;
5849  bool keep_searching = true;
5850  for (temp = arg2->data_type; temp != NULL && keep_searching; temp = temp->next)
5851  {
5852  if (temp->type_enum == PT_TYPE_NULL)
5853  {
5854  continue;
5855  }
5856 
5857  switch (common_type)
5858  {
5859  case PT_TYPE_CHAR:
5860  case PT_TYPE_NCHAR:
5861  case PT_TYPE_BIT:
5862  /* CHAR, NCHAR and BIT are common types only if all arguments are of type NCHAR or CHAR */
5863  assert (temp->type_enum == PT_TYPE_CHAR || temp->type_enum == PT_TYPE_NCHAR);
5864  if (precision > temp->info.data_type.precision)
5865  {
5867  }
5868  break;
5869  case PT_TYPE_VARCHAR:
5870  case PT_TYPE_VARNCHAR:
5871  /* either all elements are already of string types or we set maximum precision */
5872  if (!PT_IS_CHAR_STRING_TYPE (temp->type_enum))
5873  {
5875  /* no need to look any further, we've already found a type for which we have to set maximum
5876  * precision */
5877  keep_searching = false;
5878  break;
5879  }
5880  if (precision > temp->info.data_type.precision)
5881  {
5883  }
5884  break;
5885  case PT_TYPE_VARBIT:
5886  /* either all elements are already of bit types or we set maximum precision */
5887  if (!PT_IS_BIT_STRING_TYPE (temp->type_enum))
5888  {
5890  /* no need to look any further, we've already found a type for which we have to set maximum
5891  * precision */
5892  keep_searching = false;
5893  break;
5894  }
5895  if (precision > temp->info.data_type.precision)
5896  {
5898  }
5899  break;
5900  case PT_TYPE_NUMERIC:
5901  /* either all elements are numeric or all are discrete numbers */
5902  if (temp->type_enum == PT_TYPE_NUMERIC)
5903  {
5904  if (precision < temp->info.data_type.precision)
5905  {
5907  }
5908  if (scale < temp->info.data_type.dec_precision)
5909  {
5911  }
5912  }
5913  else if (PT_IS_DISCRETE_NUMBER_TYPE (temp->type_enum))
5914  {
5916  {
5918  }
5919  }
5920  else
5921  {
5922  assert (false);
5923  }
5924  break;
5925 
5926  default:
5927  assert (false);
5928  break;
5929  }
5930  }
5931  data_type->info.data_type.precision = precision;
5932  data_type->info.data_type.dec_precision = scale;
5933  }
5934 
5935  arg2 = pt_wrap_collection_with_cast_op (parser, arg2, sig.arg2_type.val.type, data_type, false);
5936  if (!arg2)
5937  {
5938  return NULL;
5939  }
5940 
5941  expr->info.expr.arg2 = arg2;
5942  }
5943  else if (PT_IS_HOSTVAR (arg2))
5944  {
5946  SET_EXPECTED_DOMAIN (arg2, d);
5947  pt_preset_hostvar (parser, arg2);
5948 
5949  error = pt_coerce_expression_argument (parser, expr, &arg1, arg1_eq_type, NULL);
5950  if (error != NO_ERROR)
5951  {
5952  return NULL;
5953  }
5954  expr->info.expr.arg1 = arg1;
5955  }
5956  else
5957  {
5958  /* This is a semantic error */
5959  return NULL;
5960  }
5961  return expr;
5962 }
5963 
5964 /*
5965  * pt_coerce_expr_arguments - apply signature sig to the arguments of the
5966  * expression expr
5967  * return : the (possibly modified) expr or NULL on error
5968  * parser(in) : the parser context
5969  * expr(in) : the SQL expression
5970  * arg1(in) : first argument of the expression
5971  * arg2(in) : second argument of the expression
5972  * arg3(in) : third argument of the expression
5973  * sig(in) : the expression signature
5974  */
5975 static PT_NODE *
5976 pt_coerce_expr_arguments (PARSER_CONTEXT * parser, PT_NODE * expr, PT_NODE * arg1, PT_NODE * arg2, PT_NODE * arg3,
5978 {
5979  PT_TYPE_ENUM arg1_type = PT_TYPE_NONE, arg2_type = PT_TYPE_NONE;
5980  PT_TYPE_ENUM arg3_type = PT_TYPE_NONE;
5981  PT_TYPE_ENUM arg1_eq_type = PT_TYPE_NONE, arg2_eq_type = PT_TYPE_NONE;
5982  PT_TYPE_ENUM arg3_eq_type = PT_TYPE_NONE;
5983  PT_TYPE_ENUM common_type = PT_TYPE_NONE;
5984  PT_NODE *arg1_dt = NULL;
5985  PT_NODE *arg2_dt = NULL;
5986  PT_NODE *arg3_dt = NULL;
5987  PT_OP_TYPE op;
5988  int error = NO_ERROR;
5989  PT_NODE *between = NULL;
5990  PT_NODE *between_ge_lt = NULL;
5991  PT_NODE *b1 = NULL;
5992  PT_NODE *b2 = NULL;
5993 
5994  op = expr->info.expr.op;
5995 
5996  arg1 = expr->info.expr.arg1;
5997  if (arg1)
5998  {
5999  arg1_type = arg1->type_enum;
6000  }
6001 
6002  arg2 = expr->info.expr.arg2;
6003  if (arg2)
6004  {
6005  arg2_type = arg2->type_enum;
6006 
6007  if (op == PT_WIDTH_BUCKET)
6008  {
6009  arg2_type = pt_get_common_arg_type_of_width_bucket (parser, expr);
6010  }
6011  }
6012 
6013  arg3 = expr->info.expr.arg3;
6014  if (arg3)
6015  {
6016  arg3_type = arg3->type_enum;
6017  }
6018 
6019  arg1_eq_type = pt_get_equivalent_type_with_op (sig.arg1_type, arg1_type, op);
6020  arg2_eq_type = pt_get_equivalent_type_with_op (sig.arg2_type, arg2_type, op);
6021  arg3_eq_type = pt_get_equivalent_type_with_op (sig.arg3_type, arg3_type, op);
6022 
6023  if (pt_is_symmetric_op (op))
6024  {
6025  if (pt_is_enumeration_special_comparison (arg1, op, arg2))
6026  {
6027  /* In case of 'ENUM = const' we need to convert the right argument to the ENUM type in order to preserve an
6028  * eventual index scan on left argument */
6029  common_type = PT_TYPE_ENUMERATION;
6030  }
6031  else
6032  {
6033  /* We should make sure that, for symmetric operators, all arguments are of the same type. */
6034  common_type = pt_infer_common_type (op, &arg1_eq_type, &arg2_eq_type, &arg3_eq_type, expr->expected_domain);
6035  }
6036 
6037  if (common_type == PT_TYPE_NONE)
6038  {
6039  /* this is an error */
6040  return NULL;
6041  }
6042 
6043  if (pt_is_range_or_comp (op)
6044  && (!PT_IS_NUMERIC_TYPE (arg1_type) || !PT_IS_NUMERIC_TYPE (arg2_type)
6045  || (arg3_type != PT_TYPE_NONE && !PT_IS_NUMERIC_TYPE (arg3_type))))
6046  {
6047  /* cast value when comparing column with value */
6048  if (PT_IS_NAME_NODE (arg1) && PT_IS_VALUE_NODE (arg2)
6049  && (arg3_type == PT_TYPE_NONE || PT_IS_VALUE_NODE (arg3)) && arg1_type != PT_TYPE_ENUMERATION)
6050  {
6051  arg1_eq_type = arg2_eq_type = arg1_type;
6052  if (arg3_type != PT_TYPE_NONE)
6053  {
6054  arg3_eq_type = arg1_type;
6055  }
6056 
6057  /* if column type is number (except NUMERIC) and operator is not EQ cast const node to DOUBLE to enhance
6058  * correctness of results */
6059  if (PT_IS_NUMERIC_TYPE (arg1_type) && arg1_type != PT_TYPE_NUMERIC && op != PT_EQ && op != PT_EQ_SOME
6060  && op != PT_EQ_ALL)
6061  {
6062  if (arg2_type != arg1_type)
6063  {
6064  arg2_eq_type = PT_TYPE_DOUBLE;
6065  }
6066  if (arg3_type != PT_TYPE_NONE && arg3_type != arg1_type)
6067  {
6068  arg3_eq_type = PT_TYPE_DOUBLE;
6069  }
6070  }
6071  }
6072  else if (PT_IS_NAME_NODE (arg2) && PT_IS_VALUE_NODE (arg1) && arg3_type == PT_TYPE_NONE
6073  && arg2_type != PT_TYPE_ENUMERATION)
6074  {
6075  arg1_eq_type = arg2_eq_type = arg2_type;
6076  if (arg1_type != arg2_type && PT_IS_NUMERIC_TYPE (arg2_type) && arg2_type != PT_TYPE_NUMERIC
6077  && op != PT_EQ && op != PT_EQ_SOME && op != PT_EQ_ALL)
6078  {
6079  arg1_eq_type = PT_TYPE_DOUBLE;
6080  }
6081  }
6082  }
6083 
6084  if (pt_is_comp_op (op))
6085  {
6086  /* do not cast between numeric types or char types for comparison operators */
6087  if (PT_ARE_COMPARABLE (arg1_eq_type, arg1_type))
6088  {
6089  arg1_eq_type = arg1_type;
6090  }
6091  if (PT_ARE_COMPARABLE (arg2_eq_type, arg2_type))
6092  {
6093  arg2_eq_type = arg2_type;
6094  }
6095  if (PT_ARE_COMPARABLE (arg3_eq_type, arg3_type))
6096  {
6097  arg3_eq_type = arg3_type;
6098  }
6099  }
6100 
6101  if (PT_IS_COLLECTION_TYPE (common_type))
6102  {
6103  /* We do not perform implicit casts on collection types. The following code will only handle constant
6104  * arguments. */
6105  if ((arg1_eq_type != PT_TYPE_NONE && !PT_IS_COLLECTION_TYPE (arg1_eq_type))
6106  || (arg2_eq_type != PT_TYPE_NONE && !PT_IS_COLLECTION_TYPE (arg2_eq_type))
6107  || (arg3_eq_type != PT_TYPE_NONE && !PT_IS_COLLECTION_TYPE (arg3_eq_type)))
6108  {
6109  return NULL;
6110  }
6111  else
6112  {
6113  if (pt_is_symmetric_type (common_type))
6114  {
6115  if (arg1_eq_type != common_type && arg1_eq_type != PT_TYPE_NONE)
6116  {
6117  pt_coerce_value (parser, arg1, arg1, common_type, PT_NODE_DATA_TYPE (arg1));
6118  arg1_type = common_type;
6119  }
6120  if (arg2_type != common_type && arg2_eq_type != PT_TYPE_NONE)
6121  {
6122  pt_coerce_value (parser, arg2, arg2, common_type, PT_NODE_DATA_TYPE (arg2));
6123  arg2_type = common_type;
6124  }
6125  }
6126  /* we're not casting collection types in this context but we should "propagate" types */
6127  if (arg2_type != PT_TYPE_NONE)
6128  {
6129  pt_propagate_types (parser, expr, PT_NODE_DATA_TYPE (arg1), PT_NODE_DATA_TYPE (arg2));
6130  }
6131  if (arg3_type != PT_TYPE_NONE)
6132  {
6133  pt_propagate_types (parser, expr, PT_NODE_DATA_TYPE (arg2), PT_NODE_DATA_TYPE (arg3));
6134  }
6135  expr->info.expr.arg1 = arg1;
6136  expr->info.expr.arg2 = arg2;
6137  expr->info.expr.arg3 = arg3;
6138  return expr;
6139  }
6140  }
6141  }
6142 
6143  /* We might have decided a new type for arg1 based on the common_type but, if the signature defines an exact type, we
6144  * should keep it. For example, + is a symmetric operator but also defines date + bigint which is not symmetric and
6145  * we have to keep the bigint type even if the common type is date. This is why, before coercing expression
6146  * arguments, we check the signature that we decided to apply */
6147  if (!sig.arg1_type.is_generic)
6148  {
6149  arg1_eq_type = sig.arg1_type.val.type;
6150  }
6152  {
6153  /* In case of recursive expression (PT_GREATEST, PT_LEAST, ...) the common type is stored in recursive_type */
6154  arg1_eq_type = expr->info.expr.recursive_type;
6155  }
6156  error = pt_coerce_expression_argument (parser, expr, &arg1, arg1_eq_type, arg1_dt);
6157  if (error != NO_ERROR)
6158  {
6159  return NULL;
6160  }
6161  else
6162  {
6163  expr->info.expr.arg1 = arg1;
6164  }
6165 
6166  if (!sig.arg2_type.is_generic)
6167  {
6168  arg2_eq_type = sig.arg2_type.val.type;
6169  }
6171  {
6172  /* In case of recursive expression (PT_GREATEST, PT_LEAST, ...) the common type is stored in recursive_type */
6173  arg2_eq_type = expr->info.expr.recursive_type;
6174  }
6175 
6176  if (op != PT_WIDTH_BUCKET)
6177  {
6178  error = pt_coerce_expression_argument (parser, expr, &arg2, arg2_eq_type, arg2_dt);
6179  if (error != NO_ERROR)
6180  {
6181  return NULL;
6182  }
6183  else
6184  {
6185  expr->info.expr.arg2 = arg2;
6186  }
6187  }
6188  else
6189  {
6190  /* width_bucket is a special case. It has 4 params the 2nd and 3rd args are coerced here */
6191  between = expr->info.expr.arg2;
6192  if (between == NULL || between->node_type != PT_EXPR || between->info.expr.op != PT_BETWEEN)
6193  {
6194  return NULL;
6195  }
6196 
6197  between_ge_lt = between->info.expr.arg2;
6198  if (between_ge_lt == NULL || between_ge_lt->node_type != PT_EXPR
6199  || between_ge_lt->info.expr.op != PT_BETWEEN_GE_LT)
6200  {
6201  return NULL;
6202  }
6203 
6204  /* 2nd, 3rd param of width_bucket */
6205  b1 = between_ge_lt->info.expr.arg1;
6206  b2 = between_ge_lt->info.expr.arg2;
6207  assert (b1 != NULL && b2 != NULL);
6208 
6209  error = pt_coerce_expression_argument (parser, between_ge_lt, &b1, arg2_eq_type, arg1_dt);
6210  if (error != NO_ERROR)
6211  {
6212  return NULL;
6213  }
6214  else
6215  {
6216  between_ge_lt->info.expr.arg1 = b1;
6217  }
6218 
6219  error = pt_coerce_expression_argument (parser, between_ge_lt, &b2, arg2_eq_type, arg2_dt);
6220  if (error != NO_ERROR)
6221  {
6222  return NULL;
6223  }
6224  else
6225  {
6226  between_ge_lt->info.expr.arg2 = b2;
6227  }
6228  }
6229 
6230  if (!sig.arg3_type.is_generic)
6231  {
6232  arg3_eq_type = sig.arg3_type.val.type;
6233  }
6234  error = pt_coerce_expression_argument (parser, expr, &arg3, arg3_eq_type, arg3_dt);
6235  if (error != NO_ERROR)
6236  {
6237  return NULL;
6238  }
6239  else
6240  {
6241  expr->info.expr.arg3 = arg3;
6242  }
6243 
6244  return expr;
6245 }
6246 
6247 /*
6248  * pt_is_range_expression () - return true if the expression is evaluated
6249  * as a logical expression
6250  * return : true if the expression is of type logical
6251  * op (in) : the expression
6252  */
6253 static bool
6254 pt_is_range_expression (const PT_OP_TYPE op)
6255 {
6256  switch (op)
6257  {
6258  case PT_GE_SOME:
6259  case PT_GT_SOME:
6260  case PT_LT_SOME:
6261  case PT_LE_SOME:
6262  case PT_GE_ALL:
6263  case PT_GT_ALL:
6264  case PT_LT_ALL:
6265  case PT_LE_ALL:
6266  case PT_EQ_SOME:
6267  case PT_NE_SOME:
6268  case PT_EQ_ALL:
6269  case PT_NE_ALL:
6270  case PT_IS_IN:
6271  case PT_IS_NOT_IN:
6272  return true;
6273  default:
6274  return false;
6275  }
6276  return false;
6277 }
6278 
6279 /*
6280  * pt_is_range_or_comp () - return true if the operator is range or comparison
6281  * return:
6282  * op(in):
6283  */
6284 static bool
6285 pt_is_range_or_comp (PT_OP_TYPE op)
6286 {
6287  switch (op)
6288  {
6289  case PT_EQ:
6290  case PT_NE:
6291  case PT_GE:
6292  case PT_GT:
6293  case PT_LT:
6294  case PT_LE:
6295  case PT_NULLSAFE_EQ:
6296  case PT_GT_INF:
6297  case PT_LT_INF:
6298  case PT_BETWEEN:
6299  case PT_BETWEEN_GE_LE:
6300  case PT_BETWEEN_GE_LT:
6301  case PT_BETWEEN_GT_LE:
6302  case PT_BETWEEN_GT_LT:
6303  case PT_BETWEEN_INF_LE:
6304  case PT_BETWEEN_INF_LT:
6305  case PT_BETWEEN_GE_INF:
6306  case PT_BETWEEN_GT_INF:
6307  return true;
6308  default:
6309  return false;
6310  }
6311 }
6312 
6313 static bool
6314 does_op_specially_treat_null_arg (PT_OP_TYPE op)
6315 {
6316  if (pt_is_operator_logical (op))
6317  {
6318  return true;
6319  }
6320 
6321  switch (op)
6322  {
6323  case PT_NVL:
6324  case PT_IFNULL:
6325  case PT_ISNULL:
6326  case PT_NVL2:
6327  case PT_COALESCE:
6328  case PT_NULLIF:
6329  case PT_TRANSLATE:
6330  case PT_RAND:
6331  case PT_DRAND:
6332  case PT_RANDOM:
6333  case PT_DRANDOM:
6334  case PT_CONCAT:
6335  case PT_CONCAT_WS:
6336  case PT_TO_CHAR:
6337  return true;
6338  case PT_REPLACE:
6340  default:
6341  return false;
6342  }
6343 }
6344 
6345 /*
6346  * pt_apply_expressions_definition () - evaluate which expression signature
6347  * best matches the received arguments
6348  * and cast arguments to the types
6349  * described in the signature
6350  * return : NO_ERROR or error code
6351  * parser(in) : the parser context
6352  * node(in/out): an SQL expression
6353  */
6354 static int
6355 pt_apply_expressions_definition (PARSER_CONTEXT * parser, PT_NODE ** node)
6356 {
6357  PT_OP_TYPE op;
6358  PT_NODE *arg1 = NULL, *arg2 = NULL, *arg3 = NULL;
6359  PT_TYPE_ENUM arg1_type = PT_TYPE_NONE, arg2_type = PT_TYPE_NONE;
6360  PT_TYPE_ENUM arg3_type = PT_TYPE_NONE;
6362  PT_NODE *expr = *node;
6363  int matches = 0, best_match = -1, i = 0;
6365 
6366  if (expr->node_type != PT_EXPR)
6367  {
6368  return NO_ERROR;
6369  }
6370  op = expr->info.expr.op;
6371  if (!pt_get_expression_definition (op, &def))
6372  {
6373  *node = NULL;
6374  return NO_ERROR;
6375  }
6376 
6378  {
6379  arg1_type = arg2_type = expr->info.expr.recursive_type;
6380  }
6381  else
6382  {
6383  arg1 = expr->info.expr.arg1;
6384  if (arg1)
6385  {
6386  arg1_type = arg1->type_enum;
6387  }
6388 
6389  arg2 = expr->info.expr.arg2;
6390  if (arg2)
6391  {
6392  arg2_type = arg2->type_enum;
6393 
6394  if (op == PT_WIDTH_BUCKET)
6395  {
6396  arg2_type = pt_get_common_arg_type_of_width_bucket (parser, expr);
6397  }
6398  }
6399  }
6400 
6401  arg3 = expr->info.expr.arg3;
6402  if (arg3)
6403  {
6404  arg3_type = arg3->type_enum;
6405  }
6406 
6407  /* check the expression contains NULL argument. If the op does not specially treat NULL args, for instance, NVL,
6408  * NVL2, IS [NOT] NULL and so on, just decide the retun type as NULL. */
6409  if (!does_op_specially_treat_null_arg (op)
6410  && ((arg1 && !arg1->is_added_by_parser && arg1_type == PT_TYPE_NULL)
6411  || (arg2 && !arg2->is_added_by_parser && arg2_type == PT_TYPE_NULL)
6412  || (arg3 && !arg3->is_added_by_parser && arg3_type == PT_TYPE_NULL)))
6413  {
6414  expr->type_enum = PT_TYPE_NULL;
6415  return NO_ERROR;
6416  }
6417 
6418  matches = -1;
6419  best_match = 0;
6420  for (i = 0; i < def.overloads_count; i++)
6421  {
6422  int match_cnt = 0;
6423  if (pt_are_unmatchable_types (def.overloads[i].arg1_type, arg1_type))
6424  {
6425  match_cnt = -1;
6426  continue;
6427  }
6428  if (pt_are_equivalent_types (def.overloads[i].arg1_type, arg1_type))
6429  {
6430  match_cnt++;
6431  }
6432  if (pt_are_unmatchable_types (def.overloads[i].arg2_type, arg2_type))
6433  {
6434  match_cnt = -1;
6435  continue;
6436  }
6437  if (pt_are_equivalent_types (def.overloads[i].arg2_type, arg2_type))
6438  {
6439  match_cnt++;
6440  }
6441  if (pt_are_unmatchable_types (def.overloads[i].arg3_type, arg3_type))
6442  {
6443  match_cnt = -1;
6444  continue;
6445  }
6446  if (pt_are_equivalent_types (def.overloads[i].arg3_type, arg3_type))
6447  {
6448  match_cnt++;
6449  }
6450  if (match_cnt == 3)
6451  {
6452  best_match = i;
6453  break;
6454  }
6455  else if (match_cnt > matches)
6456  {
6457  matches = match_cnt;
6458  best_match = i;
6459  }
6460  }
6461 
6462  if (best_match == -1)
6463  {
6464  /* if best_match is -1 then we have an expression definition but it cannot be applied on this arguments. */
6465  expr->node_type = PT_NODE_NONE;
6466  return ER_FAILED;
6467  }
6468 
6469  sig = def.overloads[best_match];
6470  if (pt_is_range_expression (op))
6471  {
6472  /* Range expressions are expressions that compare an argument with a subquery or a collection. We handle these
6473  * expressions separately */
6474  expr = pt_coerce_range_expr_arguments (parser, expr, arg1, arg2, arg3, sig);
6475  }
6476  else
6477  {
6478  expr = pt_coerce_expr_arguments (parser, expr, arg1, arg2, arg3, sig);
6479  }
6480  if (expr == NULL)
6481  {
6482  return ER_FAILED;
6483  }
6484 
6485  if (pt_is_op_hv_late_bind (op)
6486  && (arg1_type == PT_TYPE_MAYBE || arg2_type == PT_TYPE_MAYBE || arg3_type == PT_TYPE_MAYBE))
6487  {
6488  expr->type_enum = PT_TYPE_MAYBE;
6489  }
6490  else
6491  {
6492  expr->type_enum = pt_expr_get_return_type (expr, sig);
6493  }
6494 
6495  /* re-read arguments to include the wrapped-cast */
6496  arg1 = expr->info.expr.arg1;
6497  arg2 = expr->info.expr.arg2;
6498 
6500  && pt_upd_domain_info (parser, arg1, arg2, op, expr->type_enum, expr) != NO_ERROR)
6501  {
6502  expr = NULL;
6503  return ER_FAILED;
6504  }
6505  *node = expr;
6506  return NO_ERROR;
6507 }
6508 
6509 /*
6510  * pt_expr_get_return_type () - get the return type of an expression based on
6511  * the expression signature and the types of its
6512  * arguments
6513  * return : the expression return type
6514  * expr(in): an SQL expression
6515  * sig(in) : the expression signature
6516  *
6517  * Remarks: This function is called after the expression signature has been
6518  * decided and the expression arguments have been wrapped with CASTs
6519  * to the type described in the signature. At this point we can
6520  * decide the return type based on the argument types which are
6521  * proper CUBRID types (i.e.: not generic types)
6522  */
6523 static PT_TYPE_ENUM
6524 pt_expr_get_return_type (PT_NODE * expr, const EXPRESSION_SIGNATURE sig)
6525 {
6526  PT_TYPE_ENUM arg1_type = PT_TYPE_NONE, arg2_type = PT_TYPE_NONE;
6527  PT_TYPE_ENUM arg3_type = PT_TYPE_NONE;
6528 
6529  if (sig.return_type.is_generic == false)
6530  {
6531  /* if the signature does not define a generic type, return the defined type */
6532  return sig.return_type.val.type;
6533  }
6534 
6535  if (expr->info.expr.arg1)
6536  {
6537  arg1_type = expr->info.expr.arg1->type_enum;
6538  if (arg1_type == PT_TYPE_MAYBE)
6539  {
6540  if (expr->info.expr.arg1->expected_domain)
6541  {
6542  /* we were able to decide an expected domain for this argument and we can use it in deciding the return
6543  * type */
6544  arg1_type = pt_db_to_type_enum (expr->info.expr.arg1->expected_domain->type->id);
6545  }
6546  }
6547  }
6548 
6549  if (expr->info.expr.arg2)
6550  {
6551  arg2_type = expr->info.expr.arg2->type_enum;
6552  if (arg2_type == PT_TYPE_MAYBE)
6553  {
6554  if (expr->info.expr.arg2->expected_domain)
6555  {
6556  /* we were able to decide an expected domain for this argument and we can use it in deciding the return
6557  * type */
6558  arg2_type = pt_db_to_type_enum (expr->info.expr.arg2->expected_domain->type->id);
6559  }
6560  }
6561  }
6562 
6563  if (expr->info.expr.arg3)
6564  {
6565  arg3_type = expr->info.expr.arg3->type_enum;
6566  if (arg3_type == PT_TYPE_MAYBE)
6567  {
6568  if (expr->info.expr.arg3->expected_domain)
6569  {
6570  /* we were able to decide an expected domain for this argument and we can use it in deciding the return
6571  * type */
6572  arg3_type = pt_db_to_type_enum (expr->info.expr.arg3->expected_domain->type->id);
6573  }
6574  }
6575  }
6576 
6577  /* the return type of the signature is a generic type */
6578  switch (sig.return_type.val.generic_type)
6579  {
6581  {
6582  /* The return type might be CHAR, VARCHAR, NCHAR or VARNCHAR. Since not all arguments are required to be of
6583  * string type, we have to infer the return type based only on the string type arguments */
6584  PT_TYPE_ENUM common_type = PT_TYPE_NONE;
6585  if (PT_IS_STRING_TYPE (arg1_type))
6586  {
6587  common_type = arg1_type;
6588  if (PT_IS_STRING_TYPE (arg2_type))
6589  {
6590  common_type = pt_common_type (arg1_type, arg2_type);
6591  if (PT_IS_STRING_TYPE (arg3_type))
6592  {
6593  common_type = pt_common_type (common_type, arg3_type);
6594  }
6595  return common_type;
6596  }
6597 
6598  if (PT_IS_STRING_TYPE (arg3_type))
6599  {
6600  common_type = pt_common_type (common_type, arg3_type);
6601  }
6602  return common_type;
6603  }
6604  /* arg1 is not string type */
6605  if (PT_IS_STRING_TYPE (arg2_type))
6606  {
6607  common_type = arg2_type;
6608  if (PT_IS_STRING_TYPE (arg3_type))
6609  {
6610  common_type = pt_common_type (common_type, arg3_type);
6611  }
6612  return common_type;
6613  }
6614 
6615  /* arg1 and arg2 are not of string type */
6616  if (PT_IS_STRING_TYPE (arg3_type))
6617  {
6618  return arg3_type;
6619  }
6620 
6621  if (common_type != PT_TYPE_NONE)
6622  {
6623  return common_type;
6624  }
6625  break;
6626  }
6627 
6629  {
6630  PT_ARG_TYPE type;
6631  type.is_generic = true;
6632  type.val.generic_type = PT_GENERIC_TYPE_NCHAR;
6633  /* if one or the arguments is of national string type the return type must be VARNCHAR, else it is VARCHAR */
6634  if (pt_are_equivalent_types (type, arg1_type) || pt_are_equivalent_types (type, arg2_type)
6635  || pt_are_equivalent_types (type, arg3_type))
6636  {
6637  return PT_TYPE_VARNCHAR;
6638  }
6639  return PT_TYPE_VARCHAR;
6640  }
6641 
6642  case PT_GENERIC_TYPE_CHAR:
6643  if (arg1_type == PT_TYPE_VARCHAR || arg2_type == PT_TYPE_VARCHAR || arg3_type == PT_TYPE_VARCHAR)
6644  {
6645  return PT_TYPE_VARCHAR;
6646  }
6647  return PT_TYPE_CHAR;
6648 
6649  case PT_GENERIC_TYPE_NCHAR:
6650  if (arg1_type == PT_TYPE_VARNCHAR || arg2_type == PT_TYPE_VARNCHAR || arg3_type == PT_TYPE_VARNCHAR)
6651  {
6652  return PT_TYPE_VARNCHAR;
6653  }
6654  return PT_TYPE_NCHAR;
6655 
6658  case PT_GENERIC_TYPE_ANY:
6659  case PT_GENERIC_TYPE_DATE:
6662  case PT_GENERIC_TYPE_BIT:
6663  {
6664  PT_TYPE_ENUM common_type = PT_TYPE_NONE;
6665  if (arg2_type == PT_TYPE_NONE
6666  || (!pt_is_symmetric_op (expr->info.expr.op) && !pt_is_op_with_forced_common_type (expr->info.expr.op)))
6667  {
6668  return arg1_type;
6669  }
6670  common_type = pt_common_type (arg1_type, arg2_type);
6671  if (arg3_type != PT_TYPE_NONE)
6672  {
6673  common_type = pt_common_type (common_type, arg3_type);
6674  }
6675 
6676  if (common_type != PT_TYPE_NONE)
6677  {
6678  return common_type;
6679  }
6680  break;
6681  }
6682  default:
6683  break;
6684  }
6685 
6686  /* we might reach this point on expressions with a single argument of value null */
6687  if (arg1_type == PT_TYPE_NULL)
6688  {
6689  return PT_TYPE_NULL;
6690  }
6691  return PT_TYPE_NONE;
6692 }
6693 
6694 /*
6695  * pt_is_symmetric_type () -
6696  * return:
6697  * type_enum(in):
6698  */
6699 static bool
6700 pt_is_symmetric_type (const PT_TYPE_ENUM type_enum)
6701 {
6702  switch (type_enum)
6703  {
6704  case PT_TYPE_INTEGER:
6705  case PT_TYPE_BIGINT:
6706  case PT_TYPE_FLOAT:
6707  case PT_TYPE_DOUBLE:
6708  case PT_TYPE_NUMERIC:
6709  case PT_TYPE_SMALLINT:
6710  case PT_TYPE_MONETARY:
6711 
6712  case PT_TYPE_LOGICAL:
6713 
6714  case PT_TYPE_SET:
6715  case PT_TYPE_MULTISET:
6716  case PT_TYPE_SEQUENCE:
6717  case PT_TYPE_OBJECT:
6718 
6719  case PT_TYPE_VARCHAR:
6720  case PT_TYPE_CHAR:
6721  case PT_TYPE_VARNCHAR:
6722  case PT_TYPE_NCHAR:
6723  case PT_TYPE_VARBIT:
6724  case PT_TYPE_BIT:
6725  return true;
6726 
6727  default:
6728  return false;
6729  }
6730 }
6731 
6732 /*
6733  * pt_is_symmetric_op () -
6734  * return:
6735  * op(in):
6736  */
6737 bool
6739 {
6740  switch (op)
6741  {
6742  case PT_FUNCTION_HOLDER:
6743  case PT_ASSIGN:
6744  case PT_GE_SOME:
6745  case PT_GT_SOME:
6746  case PT_LT_SOME:
6747  case PT_LE_SOME:
6748  case PT_GE_ALL:
6749  case PT_GT_ALL:
6750  case PT_LT_ALL:
6751  case PT_LE_ALL:
6752  case PT_EQ_SOME:
6753  case PT_NE_SOME:
6754  case PT_EQ_ALL:
6755  case PT_NE_ALL:
6756  case PT_IS_IN:
6757  case PT_IS_NOT_IN:
6758  case PT_IS_NULL:
6759  case PT_IS_NOT_NULL:
6760  case PT_POSITION:
6761  case PT_FINDINSET:
6762  case PT_SUBSTRING:
6763  case PT_SUBSTRING_INDEX:
6764  case PT_OCTET_LENGTH:
6765  case PT_BIT_LENGTH:
6766  case PT_CHAR_LENGTH:
6767  case PT_BIN:
6768  case PT_TRIM:
6769  case PT_LTRIM:
6770  case PT_RTRIM:
6771  case PT_LIKE_LOWER_BOUND:
6772  case PT_LIKE_UPPER_BOUND:
6773  case PT_LPAD:
6774  case PT_RPAD:
6775  case PT_REPEAT:
6776  case PT_REPLACE:
6777  case PT_TRANSLATE:
6778  case PT_ADD_MONTHS:
6779  case PT_LAST_DAY:
6780  case PT_MONTHS_BETWEEN:
6781  case PT_SYS_DATE:
6782  case PT_CURRENT_DATE:
6783  case PT_SYS_TIME:
6784  case PT_CURRENT_TIME:
6785  case PT_SYS_TIMESTAMP:
6786  case PT_CURRENT_TIMESTAMP:
6787  case PT_SYS_DATETIME:
6788  case PT_CURRENT_DATETIME:
6789  case PT_UTC_TIME:
6790  case PT_UTC_DATE:
6791  case PT_TO_CHAR:
6792  case PT_TO_DATE:
6793  case PT_TO_TIME:
6794  case PT_TO_TIMESTAMP:
6795  case PT_TO_DATETIME:
6796  case PT_TO_NUMBER:
6797  case PT_CURRENT_VALUE:
6798  case PT_NEXT_VALUE:
6799  case PT_CAST:
6800  case PT_EXTRACT:
6801  case PT_INST_NUM:
6802  case PT_ROWNUM:
6803  case PT_ORDERBY_NUM:
6804  case PT_CONNECT_BY_ISCYCLE:
6805  case PT_CONNECT_BY_ISLEAF:
6806  case PT_LEVEL:
6807  case PT_CONNECT_BY_ROOT:
6809  case PT_QPRIOR:
6810  case PT_CURRENT_USER:
6812  case PT_CHR:
6813  case PT_ROUND:
6814  case PT_TRUNC:
6815  case PT_INSTR:
6816  case PT_TIME_FORMAT:
6817  case PT_TIMESTAMP:
6818  case PT_TIMEF:
6819  case PT_YEARF:
6820  case PT_MONTHF:
6821  case PT_DAYF:
6822  case PT_DAYOFMONTH:
6823  case PT_HOURF:
6824  case PT_MINUTEF:
6825  case PT_SECONDF:
6826  case PT_QUARTERF:
6827  case PT_WEEKDAY:
6828  case PT_DAYOFWEEK:
6829  case PT_DAYOFYEAR:
6830  case PT_TODAYS:
6831  case PT_FROMDAYS:
6832  case PT_TIMETOSEC:
6833  case PT_SECTOTIME:
6834  case PT_WEEKF:
6835  case PT_MAKETIME:
6836  case PT_MAKEDATE:
6837  case PT_ADDTIME:
6838  case PT_SCHEMA:
6839  case PT_DATABASE:
6840  case PT_VERSION:
6841  case PT_UNIX_TIMESTAMP:
6842  case PT_FROM_UNIXTIME:
6843  case PT_IS:
6844  case PT_IS_NOT:
6845  case PT_CONCAT:
6846  case PT_CONCAT_WS:
6847  case PT_FIELD:
6848  case PT_LEFT:
6849  case PT_RIGHT:
6850  case PT_LOCATE:
6851  case PT_MID:
6852  case PT_REVERSE:
6853  case PT_DISK_SIZE:
6854  case PT_ADDDATE:
6855  case PT_DATE_ADD:
6856  case PT_SUBDATE:
6857  case PT_DATE_SUB:
6858  case PT_FORMAT:
6859  case PT_ATAN2:
6860  case PT_DATE_FORMAT:
6861  case PT_USER:
6862  case PT_STR_TO_DATE:
6863  case PT_LIST_DBS:
6864  case PT_SYS_GUID:
6865  case PT_IF:
6866  case PT_POWER:
6867  case PT_BIT_TO_BLOB:
6868  case PT_BLOB_FROM_FILE:
6869  case PT_BLOB_LENGTH:
6870  case PT_BLOB_TO_BIT:
6871  case PT_CHAR_TO_BLOB:
6872  case PT_CHAR_TO_CLOB:
6873  case PT_CLOB_FROM_FILE:
6874  case PT_CLOB_LENGTH:
6875  case PT_CLOB_TO_CHAR:
6876  case PT_TYPEOF:
6877  case PT_INDEX_CARDINALITY:
6878  case PT_INCR:
6879  case PT_DECR:
6880  case PT_RAND:
6881  case PT_RANDOM:
6882  case PT_DRAND:
6883  case PT_DRANDOM:
6884  case PT_PI:
6885  case PT_ROW_COUNT:
6886  case PT_LAST_INSERT_ID:
6887  case PT_ABS:
6888  case PT_BETWEEN_EQ_NA:
6889  case PT_BETWEEN_GE_INF:
6890  case PT_BETWEEN_GT_INF:
6891  case PT_BETWEEN_INF_LE:
6892  case PT_BETWEEN_INF_LT:
6893  case PT_LT_INF:
6894  case PT_GT_INF:
6895  case PT_CASE:
6896  case PT_DECODE:
6897  case PT_LIKE_ESCAPE:
6898  case PT_RLIKE:
6899  case PT_NOT_RLIKE:
6900  case PT_RLIKE_BINARY:
6901  case PT_NOT_RLIKE_BINARY:
6902  case PT_EVALUATE_VARIABLE:
6903  case PT_DEFINE_VARIABLE:
6904  case PT_EXEC_STATS:
6905  case PT_CONV:
6906  case PT_IFNULL:
6907  case PT_NVL:
6908  case PT_NVL2:
6909  case PT_COALESCE:
6911  case PT_CHARSET:
6912  case PT_COERCIBILITY:
6913  case PT_COLLATION:
6914  case PT_WIDTH_BUCKET:
6915  case PT_TRACE_STATS:
6916  case PT_SHA_ONE:
6917  case PT_SHA_TWO:
6918  case PT_AES_ENCRYPT:
6919  case PT_AES_DECRYPT:
6920  case PT_INDEX_PREFIX:
6921  case PT_SLEEP:
6922  case PT_DBTIMEZONE:
6923  case PT_SESSIONTIMEZONE:
6924  case PT_TZ_OFFSET:
6925  case PT_NEW_TIME:
6926  case PT_FROM_TZ:
6927  case PT_TO_DATETIME_TZ:
6928  case PT_TO_TIMESTAMP_TZ:
6929  case PT_UTC_TIMESTAMP:
6930  case PT_CRC32:
6931  case PT_SCHEMA_DEF:
6932  case PT_CONV_TZ:
6933  case PT_JSON_CONTAINS:
6934  case PT_JSON_TYPE:
6935  case PT_JSON_EXTRACT:
6936  case PT_JSON_VALID:
6937  case PT_JSON_LENGTH:
6938  case PT_JSON_DEPTH:
6939  case PT_JSON_SEARCH:
6940  case PT_JSON_PRETTY:
6941  return false;
6942 
6943  default:
6944  return true;
6945  }
6946 }
6947 
6948 /*
6949  * pt_propagate_types () - propagate datatypes upward to this expr node
6950  * return: expr's new data_type if all OK, NULL otherwise
6951  * parser(in): the parser context
6952  * expr(in/out): expr node needing the data_type decoration
6953  * otype1(in): data_type of expr's 1st operand
6954  * otype2(in): data_type of expr's 2nd operand
6955  */
6956 
6957 static PT_NODE *
6958 pt_propagate_types (PARSER_CONTEXT * parser, PT_NODE * expr, PT_NODE * otype1, PT_NODE * otype2)
6959 {
6960  PT_NODE *o1, *o2;
6961 
6962  assert (parser != NULL);
6963 
6964  if (!expr || !otype1 || !otype2)
6965  {
6966  return NULL;
6967  }
6968 
6969  o1 = parser_copy_tree_list (parser, otype1);
6970  o2 = parser_copy_tree_list (parser, otype2);
6971 
6972  /* append one to the other */
6973  if (expr->data_type)
6974  {
6976  }
6977 
6978  expr->data_type = parser_append_node (o2, o1);
6979 
6980  return expr->data_type;
6981 }
6982 
6983 /*
6984  * pt_union_sets () - compute result = set1 + set2
6985  * return: 1 if all OK, 0 otherwise
6986  * parser(in): the parser context
6987  * domain(in): tp_domain of result
6988  * set1(in): a set/multiset db_value
6989  * set2(in): a set/multiset db_value
6990  * result(out): an empty db_value container
6991  * o2(in): a PT_NODE containing the source line & column number of set2
6992  * (used purely for generating error messages)
6993  */
6994 
6995 static int
6996 pt_union_sets (PARSER_CONTEXT * parser, TP_DOMAIN * domain, DB_VALUE * set1, DB_VALUE * set2, DB_VALUE * result,
6997  PT_NODE * o2)
6998 {
6999  DB_SET *set, *s1, *s2;
7000  int error;
7001 
7002  assert (parser != NULL && set1 != NULL && set2 != NULL && result != NULL);
7003 
7004  s1 = db_get_set (set1);
7005  s2 = db_get_set (set2);
7006  error = set_union (s1, s2, &set, domain);
7007  if (error < 0)
7008  {
7009  PT_ERRORc (parser, o2, db_error_string (3));
7010  }
7011 
7012  set_make_collection (result, set);
7013 
7014  return (!pt_has_error (parser));
7015 }
7016 
7017 /*
7018  * pt_difference_sets () - compute result = set1 - set2
7019  * return: 1 if all OK, 0 otherwise
7020  * parser(in): the parser context
7021  * domain(in): tp_domain of result
7022  * set1(in): a set/multiset db_value
7023  * set2(in): a set/multiset db_value
7024  * result(out): an empty db_value container
7025  * o2(in): a PT_NODE containing the source line & column number of set1
7026  * (used purely for generating error messages)
7027  */
7028 
7029 static int
7030 pt_difference_sets (PARSER_CONTEXT * parser, TP_DOMAIN * domain, DB_VALUE * set1, DB_VALUE * set2, DB_VALUE * result,
7031  PT_NODE * o2)
7032 {
7033  DB_SET *set, *s1, *s2;
7034  int error;
7035 
7036  assert (parser != NULL && set1 != NULL && set2 != NULL && result != NULL);
7037 
7038  s1 = db_get_set (set1);
7039  s2 = db_get_set (set2);
7040  error = set_difference (s1, s2, &set, domain);
7041  if (error < 0)
7042  {
7043  PT_ERRORc (parser, o2, db_error_string (3));
7044  }
7045 
7046  set_make_collection (result, set);
7047 
7048  return (!pt_has_error (parser));
7049 }
7050 
7051 /*
7052  * pt_product_sets () - compute result = set1 * set2
7053  * return: 1 if all OK, 0 otherwise
7054  * parser(in): the parser context
7055  * domain(in): tp_domain of result
7056  * set1(in): a set/multiset db_value
7057  * set2(in): a set/multiset db_value
7058  * result(out): an empty db_value container
7059  * o2(in): a PT_NODE containing the source line & column number of set1
7060  * (used purely for generating error messages)
7061  */
7062 static int
7063 pt_product_sets (PARSER_CONTEXT * parser, TP_DOMAIN * domain, DB_VALUE * set1, DB_VALUE * set2, DB_VALUE * result,
7064  PT_NODE * o2)
7065 {
7066  DB_SET *set, *s1, *s2;
7067  int error;
7068 
7069  assert (parser != NULL && set1 != NULL && set2 != NULL && result != NULL);
7070 
7071  s1 = db_get_set (set1);
7072  s2 = db_get_set (set2);
7073  error = set_intersection (s1, s2, &set, domain);
7074  if (error < 0)
7075  {
7076  PT_ERRORc (parser, o2, db_error_string (3));
7077  }
7078 
7079  set_make_collection (result, set);
7080 
7081  return (!pt_has_error (parser));
7082 }
7083 
7084 
7085 /*
7086  * pt_where_type () - Test for constant folded where clause,
7087  * and fold as necessary
7088  * return:
7089  * parser(in):
7090  * where(in/out):
7091  *
7092  * Note :
7093  * Unfortunately, NULL is allowed in this test to provide
7094  * for constant folded clauses.
7095  *
7096  */
7097 
7098 PT_NODE *
7100 {
7101  PT_NODE *cnf_node, *dnf_node, *cnf_prev, *dnf_prev;
7102  bool cut_off;
7103  int line = 0, column = 0;
7104  short location;
7105 
7106  if (where)
7107  {
7108  line = where->line_number;
7109  column = where->column_number;
7110  }
7111 
7112  /* traverse CNF list and keep track the pointer to previous node */
7113  cnf_prev = NULL;
7114  while ((cnf_node = ((cnf_prev) ? cnf_prev->next : where)))
7115  {
7116  /* save location */
7117  location = 0;
7118  switch (cnf_node->node_type)
7119  {
7120  case PT_EXPR:
7121  location = cnf_node->info.expr.location;
7122  break;
7123 
7124  case PT_VALUE:
7125  location = cnf_node->info.value.location;
7126  break;
7127 
7128  case PT_HOST_VAR:
7129  /* TRUE/FALSE can be bound */
7130  break;
7131 
7132  default:
7133  /* stupid where cond. treat it as false condition example: SELECT * FROM foo WHERE id; */
7134  goto always_false;
7135  }
7136 
7137  if (cnf_node->type_enum == PT_TYPE_NA || cnf_node->type_enum == PT_TYPE_NULL)
7138  {
7139  /* on_cond does not confused with where conjunct is a NULL, treat it as false condition */
7140  goto always_false;
7141  }
7142 
7143  if (cnf_node->type_enum != PT_TYPE_MAYBE && cnf_node->type_enum != PT_TYPE_LOGICAL
7144  && !(cnf_node->type_enum == PT_TYPE_NA || cnf_node->type_enum == PT_TYPE_NULL))
7145  {
7146  /* If the conjunct is not a NULL or a logical type, then there's a problem. But don't say anything if
7147  * somebody else has already complained */
7148  if (!pt_has_error (parser))
7149  {
7151  }
7152  break;
7153  }
7154 
7155  cut_off = false;
7156 
7157  if (cnf_node->or_next == NULL)
7158  {
7159  if (cnf_node->node_type == PT_VALUE && cnf_node->type_enum == PT_TYPE_LOGICAL
7160  && cnf_node->info.value.data_value.i == 1)
7161  {
7162  cut_off = true;
7163  }
7164  else
7165  {
7166  /* do not fold node which already have been folded */
7167  if (cnf_node->node_type == PT_VALUE && cnf_node->type_enum == PT_TYPE_LOGICAL
7168  && cnf_node->info.value.data_value.i == 0)
7169  {
7170  if (cnf_node == where && cnf_node->next == NULL)
7171  {
7172  return where;
7173  }
7174  goto always_false;
7175  }
7176  }
7177  }
7178  else
7179  {
7180  /* traverse DNF list and keep track of the pointer to previous node */
7181  dnf_prev = NULL;
7182  while ((dnf_node = ((dnf_prev) ? dnf_prev->or_next : cnf_node)))
7183  {
7184  if (dnf_node->node_type == PT_VALUE && dnf_node->type_enum == PT_TYPE_LOGICAL
7185  && dnf_node->info.value.data_value.i == 1)
7186  {
7187  cut_off = true;
7188  break;
7189  }
7190 
7191  if (dnf_node->node_type == PT_VALUE && dnf_node->type_enum == PT_TYPE_LOGICAL
7192  && dnf_node->info.value.data_value.i == 0)
7193  {
7194  /* cut it off from DNF list */
7195  if (dnf_prev)
7196  {
7197  dnf_prev->or_next = dnf_node->or_next;
7198  }
7199  else
7200  {
7201  if (cnf_prev)
7202  {
7203  if (dnf_node->or_next)
7204  {
7205  cnf_prev->next = dnf_node->or_next;
7206  dnf_node->or_next->next = dnf_node->next;
7207  }
7208  else
7209  {
7210  goto always_false;
7211  }
7212 
7213  cnf_node = cnf_prev->next;
7214  }
7215  else
7216  {
7217  if (dnf_node->or_next)
7218  {
7219  where = dnf_node->or_next;
7220  dnf_node->or_next->next = dnf_node->next;
7221  }
7222  else
7223  {
7224  goto always_false;
7225  }
7226 
7227  cnf_node = where;
7228  } /* else (cnf_prev) */
7229  } /* else (dnf_prev) */
7230  dnf_node->next = NULL;
7231  dnf_node->or_next = NULL;
7232  parser_free_tree (parser, dnf_node);
7233  if (where == NULL)
7234  {
7235  goto always_false;
7236  }
7237  continue;
7238  }
7239 
7240  dnf_prev = (dnf_prev) ? dnf_prev->or_next : dnf_node;
7241  } /* while (dnf_node) */
7242  } /* else (cnf_node->or_next == NULL) */
7243 
7244  if (cut_off)
7245  {
7246  /* cut if off from CNF list */
7247  if (cnf_prev)
7248  {
7249  cnf_prev->next = cnf_node->next;
7250  }
7251  else
7252  {
7253  where = cnf_node->next;
7254  }
7255  cnf_node->next = NULL;
7256  parser_free_tree (parser, cnf_node);
7257  }
7258  else
7259  {
7260  cnf_prev = (cnf_prev) ? cnf_prev->next : cnf_node;
7261  }
7262  } /* while (cnf_node) */
7263 
7264  return where;
7265 
7266 always_false:
7267 
7268  /* If any conjunct is false, the entire WHERE clause is false. Jack the return value to be a single false node (being
7269  * sure to unlink the node from the "next" chain if we reuse the incoming node). */
7270  parser_free_tree (parser, where);
7271  where = parser_new_node (parser, PT_VALUE);
7272  if (where == NULL)
7273  {
7274  PT_INTERNAL_ERROR (parser, "allocate new node");
7275  return NULL;
7276  }
7277 
7278  where->line_number = line;
7279  where->column_number = column;
7280  where->type_enum = PT_TYPE_LOGICAL;
7281  where->info.value.data_value.i = 0;
7282  where->info.value.location = location;
7283  (void) pt_value_to_db (parser, where);
7284 
7285  return where;
7286 }
7287 
7288 /*
7289  * pt_where_type_keep_true () - The same as pt_where_type but if the expression
7290  * is true it is folded to a true value rather than a NULL.
7291  */
7292 PT_NODE *
7294 {
7295  PT_NODE *save_where = where;
7296 
7297  where = pt_where_type (parser, where);
7298  if (where == NULL && save_where != NULL)
7299  {
7300  /* TODO: The line/column number is lost. */
7301  where = parser_new_node (parser, PT_VALUE);
7302  if (where == NULL)
7303  {
7304  PT_INTERNAL_ERROR (parser, "allocate new node");
7305  return NULL;
7306  }
7307  where->type_enum = PT_TYPE_LOGICAL;
7308  where->info.value.data_value.i = 1;
7309  (void) pt_value_to_db (parser, where);
7310  }
7311  return where;
7312 }
7313 
7314 /*
7315  * pt_false_where () - Test for constant folded where in select
7316  * that evaluated false. also check that it is not an aggregate select
7317  * return:
7318  * parser(in):
7319  * node(in):
7320  */
7321 
7322 bool
7324 {
7325  PT_NODE *from, *where;
7326 
7327  where = NULL;
7328 
7329  switch (node->node_type)
7330  {
7331  case PT_VALUE:
7332  where = node;
7333  break;
7334 
7335  case PT_SELECT:
7336 
7337  /* If the "connect by" condition is false the query still has to return all the "start with" tuples. Therefore we
7338  * do not check that "connect by" is false. */
7339  if (node->info.query.q.select.start_with)
7340  {
7341  where = node->info.query.q.select.start_with;
7342  if (pt_false_search_condition (parser, where) == true)
7343  {
7344  return true;
7345  }
7346  }
7347  if (node->info.query.q.select.after_cb_filter)
7348  {
7349  where = node->info.query.q.select.after_cb_filter;
7350  if (pt_false_search_condition (parser, where) == true)
7351  {
7352  return true;
7353  }
7354  }
7355 
7356  if (node->info.query.orderby_for)
7357  {
7358  where = node->info.query.orderby_for;
7359  if (pt_false_search_condition (parser, where) == true)
7360  {
7361  return true;
7362  }
7363  }
7364 
7366  {
7367  return false;
7368  }
7369 
7370  if (node->info.query.q.select.group_by)
7371  {
7372  where = node->info.query.q.select.having;
7373  if (pt_false_search_condition (parser, where) == true)
7374  {
7375  return true;
7376  }
7377  }
7378 
7379  for (from = node->info.query.q.select.from; from; from = from->next)
7380  {
7381  /* exclude outer join spec from folding */
7383  || (from->next
7384  && ((from->next->info.spec.join_type == PT_JOIN_LEFT_OUTER)
7385  || (from->next->info.spec.join_type == PT_JOIN_RIGHT_OUTER))))
7386  {
7387  continue;
7388  }
7389  else if (from->info.spec.derived_table_type == PT_IS_SUBQUERY
7391  {
7392  PT_NODE *derived_table;
7393 
7394  derived_table = from->info.spec.derived_table;
7395  if (PT_IS_FALSE_WHERE_VALUE (derived_table))
7396  {
7397  return true;
7398  }
7399  }
7400  else if (PT_SPEC_IS_CTE (from))
7401  {
7402  PT_NODE *cte = from->info.spec.cte_pointer;
7403  PT_NODE *cte_non_recursive;
7404 
7405  CAST_POINTER_TO_NODE (cte);
7406 
7407  if (cte)
7408  {
7409  cte_non_recursive = cte->info.cte.non_recursive_part;
7410 
7411  if (PT_IS_FALSE_WHERE_VALUE (cte_non_recursive))
7412  {
7413  return true;
7414  }
7415  }
7416  }
7417  }
7418 
7419  where = node->info.query.q.select.where;
7420  break;
7421 
7422  case PT_UPDATE:
7423  where = node->info.update.search_cond;
7424  break;
7425 
7426  case PT_DELETE:
7427  where = node->info.delete_.search_cond;
7428  break;
7429 
7430  case PT_MERGE:
7431  where = node->info.merge.search_cond;
7432  break;
7433 
7434  default:
7435  break;
7436  }
7437 
7438  return pt_false_search_condition (parser, where);
7439 }
7440 
7441 
7442 /*
7443  * pt_false_search_condition () - Test for constant-folded search condition
7444  * that evaluated false
7445  * return: 1 if any of the conjuncts are effectively false, 0 otherwise
7446  * parser(in):
7447  * node(in):
7448  */
7449 
7450 bool
7452 {
7453  while (node)
7454  {
7455  if (node->or_next == NULL
7456  && (node->type_enum == PT_TYPE_NA || node->type_enum == PT_TYPE_NULL
7457  || (node->node_type == PT_VALUE && node->type_enum == PT_TYPE_LOGICAL
7458  && node->info.value.data_value.i == 0)))
7459  {
7460  return true;
7461  }
7462 
7463  node = node->next;
7464  }
7465 
7466  return false;
7467 }
7468 
7469 /*
7470  * pt_to_false_subquery () -
7471  * return:
7472  * parser(in):
7473  * node(in/out):
7474  */
7475 static PT_NODE *
7476 pt_to_false_subquery (PARSER_CONTEXT * parser, PT_NODE * node)
7477 {
7478  PT_NODE *next;
7479  int line, column;
7480  const char *alias_print;
7481  PT_SELECT_INFO *subq;
7482  int col_cnt, i;
7483  PT_NODE *col, *set, *spec;
7484 
7485  if (node->info.query.has_outer_spec == 1 || node->info.query.is_sort_spec || node->info.query.is_insert_select)
7486  {
7487  /* rewrite as empty subquery for example, SELECT a, b FROM x LEFT OUTER JOIN y WHERE 0 <> 0 => SELECT null, null
7488  * FROM table({}) as av6749(av_1) WHERE 0 <> 0 */
7489 
7490  if (node->node_type != PT_SELECT)
7491  {
7492  return NULL;
7493  }
7494 
7495  subq = &(node->info.query.q.select);
7496 
7497  /* rewrite SELECT list */
7499 
7500  parser_free_tree (parser, subq->list);
7501  subq->list = NULL;
7502 
7503  for (i = 0; i < col_cnt; i++)
7504  {
7505  col = parser_new_node (parser, PT_VALUE);
7506  if (col)
7507  {
7508  col->type_enum = PT_TYPE_NULL;
7509  subq->list = parser_append_node (subq->list, col);
7510  }
7511  else
7512  {
7514  return NULL;
7515  }
7516  }
7517 
7518  /* rewrite FROM list */
7519  set = parser_new_node (parser, PT_VALUE);
7520  spec = parser_new_node (parser, PT_SPEC);
7521  if (set && spec)
7522  {
7523  parser_free_tree (parser, subq->from);
7524  subq->from = NULL;
7525 
7526  set->type_enum = PT_TYPE_SEQUENCE;
7527 
7528  spec->info.spec.id = (UINTPTR) spec;
7529  spec->info.spec.derived_table = set;
7531 
7532  /* set line number to dummy class, dummy attr */
7533  spec->info.spec.range_var = pt_name (parser, "av6749");
7534  spec->info.spec.as_attr_list = pt_name (parser, "av_1");
7535 
7536  if (spec->info.spec.as_attr_list)
7537  {
7539  }
7540  subq->from = spec;
7541  }
7542  else
7543  {
7545  return NULL;
7546  }
7547 
7548  /* clear unnecessary node info */
7549  if (node->info.query.order_by && !node->info.query.q.select.connect_by)
7550  {
7551  parser_free_tree (parser, node->info.query.order_by);
7552  node->info.query.order_by = NULL;
7553  }
7554 
7555  if (node->info.query.orderby_for)
7556  {
7557  parser_free_tree (parser, node->info.query.orderby_for);
7558  node->info.query.orderby_for = NULL;
7559  }
7560 
7561  node->info.query.correlation_level = 0;
7562  node->info.query.all_distinct = PT_ALL;
7563 
7564  /* clear unnecessary subq info */
7565  if (subq->group_by)
7566  {
7567  parser_free_tree (parser, subq->group_by);
7568  subq->group_by = NULL;
7569  }
7570 
7571  if (subq->connect_by)
7572  {
7574  subq->connect_by = NULL;
7575  }
7576 
7577  if (subq->start_with)
7578  {
7580  subq->start_with = NULL;
7581  }
7582 
7583  if (subq->after_cb_filter)
7584  {
7586  subq->after_cb_filter = NULL;
7587  }
7588 
7589  if (subq->having)
7590  {
7591  parser_free_tree (parser, subq->having);
7592  subq->having = NULL;
7593  }
7594 
7595  if (subq->using_index)
7596  {
7598  subq->using_index = NULL;
7599  }
7600 
7601  subq->hint = PT_HINT_NONE;
7602  }
7603  else
7604  {
7605  int hidden = node->is_hidden_column;
7606 
7607  /* rewrite as null value */
7608  next = node->next;
7609  line = node->line_number;
7610  column = node->column_number;
7611  alias_print = node->alias_print;
7612 
7613  node->next = NULL;
7615 
7617  if (!node)
7618  {
7620  return NULL;
7621  }
7622 
7623  node->line_number = line;
7624  node->column_number = column;
7625  node->alias_print = alias_print;
7626  node->type_enum = PT_TYPE_NULL;
7627  node->info.value.location = 0;
7628  node->is_hidden_column = hidden;
7629  node->next = next; /* restore link */
7630  }
7631 
7632  return node;
7633 }
7634 
7635 /*
7636  * pt_eval_recursive_expr_type () - evaluates type for recursive expression nodes.
7637  * return: evaluated node
7638  * parser(in):
7639  * recursive_expr(in): recursive expression node to evaluate.
7640  */
7641 static PT_NODE *
7642 pt_eval_recursive_expr_type (PARSER_CONTEXT * parser, PT_NODE * recursive_expr)
7643 {
7644  PT_OP_TYPE op;
7645 
7646  if (recursive_expr == NULL)
7647  {
7648  return NULL;
7649  }
7650 
7651  if (PT_IS_RECURSIVE_EXPRESSION (recursive_expr))
7652  {
7653  op = recursive_expr->info.expr.op;
7654  if (PT_IS_LEFT_RECURSIVE_EXPRESSION (recursive_expr))
7655  {
7656  if (recursive_expr->info.expr.arg1 != NULL && PT_IS_RECURSIVE_EXPRESSION (recursive_expr->info.expr.arg1)
7657  && op == recursive_expr->info.expr.arg1->info.expr.op)
7658  {
7659  recursive_expr->info.expr.arg1 = pt_eval_recursive_expr_type (parser, recursive_expr->info.expr.arg1);
7660  }
7661  }
7662  else
7663  {
7664  if (recursive_expr->info.expr.arg2 != NULL && PT_IS_RECURSIVE_EXPRESSION (recursive_expr->info.expr.arg2)
7665  && op == recursive_expr->info.expr.arg2->info.expr.op)
7666  {
7667  recursive_expr->info.expr.arg2 = pt_eval_recursive_expr_type (parser, recursive_expr->info.expr.arg2);
7668  }
7669  }
7670  }
7671 
7672  return pt_eval_expr_type (parser, recursive_expr);
7673 }
7674 
7675 /*
7676  * pt_eval_type_pre () -
7677  * return:
7678  * parser(in):
7679  * node(in):
7680  * arg(in):
7681  * continue_walk(in):
7682  */
7683 static PT_NODE *
7684 pt_eval_type_pre (PARSER_CONTEXT * parser, PT_NODE * node, void *arg, int *continue_walk)
7685 {
7686  PT_NODE *arg1, *arg2;
7687  PT_NODE *derived_table;
7688  SEMANTIC_CHK_INFO *sc_info = (SEMANTIC_CHK_INFO *) arg;
7689 
7690  /* To ensure that after exit of recursive expression node the evaluation of type will continue in normal mode */
7691  *continue_walk = PT_CONTINUE_WALK;
7692  if (sc_info->donot_fold == true)
7693  { /* skip folding */
7694  return node;
7695  }
7696 
7697  switch (node->node_type)
7698  {
7699  case PT_SPEC:
7700  derived_table = node->info.spec.derived_table;
7701  if (pt_is_query (derived_table))
7702  {
7703  /* exclude outer join spec from folding */
7704  if (node->info.spec.join_type == PT_JOIN_LEFT_OUTER || node->info.spec.join_type == PT_JOIN_RIGHT_OUTER
7705  || (node->next && (node->next->info.spec.join_type == PT_JOIN_LEFT_OUTER
7706  || (node->next->info.spec.join_type == PT_JOIN_RIGHT_OUTER))))
7707  {
7708  derived_table->info.query.has_outer_spec = 1;
7709  }
7710  else
7711  {
7712  derived_table->info.query.has_outer_spec = 0;
7713  }
7714  }
7715  break;
7716 
7717  case PT_SORT_SPEC:
7718  /* if sort spec expression is query, mark it as such */
7719  if (node->info.sort_spec.expr && PT_IS_QUERY (node->info.sort_spec.expr))
7720  {
7721  node->info.sort_spec.expr->info.query.is_sort_spec = 1;
7722  }
7723  break;
7724 
7725  case PT_UNION:
7726  case PT_DIFFERENCE:
7727  case PT_INTERSECTION:
7728  /* propagate to children */
7729  arg1 = node->info.query.q.union_.arg1;
7730  arg2 = node->info.query.q.union_.arg2;
7731  arg1->info.query.has_outer_spec = node->info.query.has_outer_spec;
7732  arg2->info.query.has_outer_spec = node->info.query.has_outer_spec;
7733 
7734  /* rewrite limit clause as numbering expression and add it to the corresponding predicate */
7735  if (node->info.query.limit && node->info.query.rewrite_limit)
7736  {
7737  PT_NODE *limit, *t_node;
7738  PT_NODE **expr_pred;
7739 
7740  /* If both ORDER BY clause and LIMIT clause are specified, we will rewrite LIMIT to ORDERBY_NUM(). For
7741  * example, (SELECT ...) UNION (SELECT ...) ORDER BY a LIMIT 10 will be rewritten to: (SELECT ...) UNION
7742  * (SELECT ...) ORDER BY a FOR ORDERBY_NUM() <= 10 If LIMIT clause is only specified, we will rewrite the
7743  * query at query optimization step. See qo_rewrite_queries() function for more information. */
7744  if (node->info.query.order_by != NULL)
7745  {
7746  expr_pred = &node->info.query.orderby_for;
7747  limit = pt_limit_to_numbering_expr (parser, node->info.query.limit, PT_ORDERBY_NUM, false);
7748  if (limit != NULL)
7749  {
7750  t_node = *expr_pred;
7751  while (t_node != NULL && t_node->next != NULL)
7752  {
7753  t_node = t_node->next;
7754  }
7755  if (t_node == NULL)
7756  {
7757  t_node = *expr_pred = limit;
7758  }
7759  else
7760  {
7761  t_node->info.expr.paren_type = 1;
7762  t_node->next = limit;
7763  }
7764 
7765  node->info.query.rewrite_limit = 0;
7766  }
7767  else
7768  {
7770  }
7771  }
7772  }
7773  break;
7774 
7775  case PT_SELECT:
7776  /* rewrite limit clause as numbering expression and add it to the corresponding predicate */
7777  if (node->info.query.limit && node->info.query.rewrite_limit)
7778  {
7779  PT_NODE *limit, *t_node;
7780  PT_NODE **expr_pred;
7781 
7782  if (node->info.query.order_by)
7783  {
7784  expr_pred = &node->info.query.orderby_for;
7785  limit = pt_limit_to_numbering_expr (parser, node->info.query.limit, PT_ORDERBY_NUM, false);
7786  }
7787  else if (node->info.query.q.select.group_by)
7788  {
7789  expr_pred = &node->info.query.q.select.having;
7790  limit = pt_limit_to_numbering_expr (parser, node->info.query.limit, PT_LAST_OPCODE, true);
7791  }
7792  else if (node->info.query.all_distinct == PT_DISTINCT)
7793  {
7794  /* When a distinct query has neither orderby nor groupby clause, limit must be orderby_num predicate. */
7795  expr_pred = &node->info.query.orderby_for;
7796  limit = pt_limit_to_numbering_expr (parser, node->info.query.limit, PT_ORDERBY_NUM, false);
7797  }
7798  else
7799  {
7800  expr_pred = &node->info.query.q.select.where;
7801  limit = pt_limit_to_numbering_expr (parser, node->info.query.limit, PT_INST_NUM, false);
7802  }
7803 
7804  if (limit != NULL)
7805  {
7806  t_node = *expr_pred;
7807  while (t_node != NULL && t_node->next != NULL)
7808  {
7809  t_node = t_node->next;
7810  }
7811  if (t_node == NULL)
7812  {
7813  t_node = *expr_pred = limit;
7814  }
7815  else
7816  {
7817  t_node->info.expr.paren_type = 1;
7818  t_node->next = limit;
7819  }
7820 
7821  node->info.query.rewrite_limit = 0;
7822  }
7823  else
7824  {
7826  }
7827  }
7828  break;
7829 
7830  case PT_INSERT:
7831  /* mark inserted sub-query as belonging to insert statement */
7832  if (node->info.insert.value_clauses->info.node_list.list_type == PT_IS_SUBQUERY)
7833  {
7834  node->info.insert.value_clauses->info.node_list.list->info.query.is_insert_select = 1;
7835  }
7836  break;
7837 
7838  case PT_DELETE:
7839  /* rewrite limit clause as numbering expression and add it to search condition */
7840  if (node->info.delete_.limit && node->info.delete_.rewrite_limit)
7841  {
7842  PT_NODE *t_node = node->info.delete_.search_cond;
7843  PT_NODE *limit = pt_limit_to_numbering_expr (parser, node->info.delete_.limit, PT_INST_NUM, false);
7844 
7845  if (limit != NULL)
7846  {
7847  while (t_node != NULL && t_node->next != NULL)
7848  {
7849  t_node = t_node->next;
7850  }
7851  if (t_node == NULL)
7852  {
7853  node->info.delete_.search_cond = limit;
7854  }
7855  else
7856  {
7857  t_node->info.expr.paren_type = 1;
7858  t_node->next = limit;
7859  }
7860 
7861  node->info.delete_.rewrite_limit = 0;
7862  }
7863  else
7864  {
7866  }
7867  }
7868  break;
7869 
7870  case PT_UPDATE:
7871  /* rewrite limit clause as numbering expression and add it to search condition */
7872  if (node->info.update.limit && node->info.update.rewrite_limit)
7873  {
7874  PT_NODE **expr_pred = NULL;
7875  PT_NODE *t_node = NULL, *limit = NULL;
7876 
7877  if (node->info.update.order_by)
7878  {
7879  expr_pred = &node->info.update.orderby_for;
7880  limit = pt_limit_to_numbering_expr (parser, node->info.update.limit, PT_ORDERBY_NUM, false);
7881  }
7882  else
7883  {
7884  expr_pred = &node->info.update.search_cond;
7885  limit = pt_limit_to_numbering_expr (parser, node->info.update.limit, PT_INST_NUM, false);
7886  }
7887 
7888  if (limit != NULL)
7889  {
7890  t_node = *expr_pred;
7891  while (t_node != NULL && t_node->next != NULL)
7892  {
7893  t_node = t_node->next;
7894  }
7895  if (t_node == NULL)
7896  {
7897  t_node = *expr_pred = limit;
7898  }
7899  else
7900  {
7901  t_node->info.expr.paren_type = 1;
7902  t_node->next = limit;
7903  }
7904 
7905  node->info.update.rewrite_limit = 0;
7906  }
7907  else
7908  {
7910  }
7911  }
7912  break;
7913 
7914  case PT_EXPR:
7915  {
7916  PT_NODE *recurs_expr = node, *node_tmp = NULL;
7917  PT_TYPE_ENUM common_type = PT_TYPE_NONE;
7918  bool has_enum = false;
7919  PT_NODE **recurs_arg = NULL, **norm_arg = NULL;
7920  PT_OP_TYPE op = recurs_expr->info.expr.op;
7921 
7922  /* Because the recursive expressions with more than two arguments are build as PT_GREATEST(PT_GREATEST(...,
7923  * argn-1), argn) we need to compute the common type between all arguments in order to give a correct return
7924  * type. Let's say we have the following call to PT_GREATEST: greatest(e1, e2, e3, 2) where e1, e2, e3 are
7925  * ENUMs. The internal form is rewrited to greatest(greatest(greatest(e1, e2), e3), 2). For the inner call the
7926  * common type will be STRING. For middle call the common type will be STRING and for the outer call the common
7927  * type will be DOUBLE and both arguments will be casted to DOUBLE including the returned STRING of the middle
7928  * (or even inner) call. If the string does not have a numeric format, the call will fail. The natural
7929  * behaviour is a conversion of all enum arguments to the type of '2' (integer). So we compute the common type
7930  * of all arguments and store it in the recursive_type member of the PT_EXPR_INFO structure and use it in
7931  * pt_eval_type function (and other places) as common type. */
7932  if (!PT_IS_RECURSIVE_EXPRESSION (node) || node->info.expr.recursive_type != PT_TYPE_NONE)
7933  {
7934  break;
7935  }
7936 
7937  while (recurs_expr != NULL && PT_IS_RECURSIVE_EXPRESSION (recurs_expr) && op == recurs_expr->info.expr.op)
7938  {
7939  if (PT_IS_LEFT_RECURSIVE_EXPRESSION (recurs_expr))
7940  {
7941  norm_arg = &recurs_expr->info.expr.arg2;
7942  recurs_arg = &recurs_expr->info.expr.arg1;
7943  }
7944  else
7945  {
7946  norm_arg = &recurs_expr->info.expr.arg1;
7947  recurs_arg = &recurs_expr->info.expr.arg2;
7948  }
7949  /* In order to correctly compute the common type we need to know the type of each argument and therefore we
7950  * compute it. */
7951  node_tmp = pt_semantic_type (parser, *norm_arg, (SEMANTIC_CHK_INFO *) arg);
7952  if (*norm_arg == NULL || pt_has_error (parser))
7953  {
7954  return node;
7955  }
7956  *norm_arg = node_tmp;
7957  if ((*norm_arg)->type_enum != PT_TYPE_ENUMERATION)
7958  {
7959  if (common_type == PT_TYPE_NONE)
7960  {
7961  common_type = (*norm_arg)->type_enum;
7962  }
7963  else
7964  {
7965  common_type = pt_common_type (common_type, (*norm_arg)->type_enum);
7966  }
7967  }
7968  else
7969  {
7970  has_enum = true;
7971  }
7972  if (*recurs_arg == NULL || !PT_IS_RECURSIVE_EXPRESSION (*recurs_arg) || op != (*recurs_arg)->info.expr.op)
7973  {
7974  node_tmp = pt_semantic_type (parser, *recurs_arg, (SEMANTIC_CHK_INFO *) arg);
7975  if (node_tmp == NULL || pt_has_error (parser))
7976  {
7977  return node;
7978  }
7979  *recurs_arg = node_tmp;
7980  if ((*recurs_arg)->type_enum != PT_TYPE_ENUMERATION)
7981  {
7982  if (common_type == PT_TYPE_NONE)
7983  {
7984  common_type = (*recurs_arg)->type_enum;
7985  }
7986  else
7987  {
7988  common_type = pt_common_type (common_type, (*recurs_arg)->type_enum);
7989  }
7990  }
7991  else
7992  {
7993  has_enum = true;
7994  }
7995  }
7996  if (recurs_expr->info.expr.arg3 != NULL)
7997  {
7998  node_tmp = pt_semantic_type (parser, recurs_expr->info.expr.arg3, (SEMANTIC_CHK_INFO *) arg);
7999  if (node_tmp == NULL || pt_has_error (parser))
8000  {
8001  return node;
8002  }
8003  recurs_expr->info.expr.arg3 = node_tmp;
8004  }
8005  recurs_expr = *recurs_arg;
8006  }
8007 
8008  if (has_enum)
8009  {
8010  if (common_type == PT_TYPE_NONE)
8011  {
8012  /* Proceed to normal type evaluation: each function node evaluates only its own arguments */
8013  break;
8014  }
8015  common_type = pt_common_type (common_type, PT_TYPE_ENUMERATION);
8016  recurs_expr = node;
8017  while (PT_IS_RECURSIVE_EXPRESSION (recurs_expr) && op == recurs_expr->info.expr.op)
8018  {
8019  recurs_expr->info.expr.recursive_type = common_type;
8020 
8021  if (PT_IS_LEFT_RECURSIVE_EXPRESSION (recurs_expr))
8022  {
8023  recurs_expr = recurs_expr->info.expr.arg1;
8024  }
8025  else
8026  {
8027  recurs_expr = recurs_expr->info.expr.arg2;
8028  }
8029  }
8030  }
8031 
8032  node = pt_eval_recursive_expr_type (parser, node);
8033 
8034  if (op == PT_DECODE || op == PT_CASE)
8035  {
8036  /* the rest of recursive expressions are checked by normal collation inference */
8037  if (pt_check_recursive_expr_collation (parser, &node) != NO_ERROR)
8038  {
8039  node = NULL;
8040  }
8041  }
8042  /* Because for recursive functions we evaluate type here, we don't need to evaluate it twice so we skip the
8043  * normal path of type evaluation */
8044  *continue_walk = PT_LIST_WALK;
8045  return node;
8046  }
8047  break;
8048 
8049  default:
8050  break;
8051  }
8052 
8053  return node;
8054 }
8055 
8056 /*
8057  * pt_fold_constants () - perform constant folding on the specified node
8058  * return : the node after constant folding
8059  *
8060  * parser(in) : the parser context
8061  * node(in) : the node to be folded
8062  * arg(in) :
8063  * continue_walk(in):
8064  */
8065 static PT_NODE *
8066 pt_fold_constants (PARSER_CONTEXT * parser, PT_NODE * node, void *arg, int *continue_walk)
8067 {
8068  SEMANTIC_CHK_INFO *sc_info = (SEMANTIC_CHK_INFO *) arg;
8069 
8070  if (node == NULL)
8071  {
8072  return node;
8073  }
8074 
8075  if (sc_info->donot_fold == true)
8076  {
8077  /* skip folding */
8078  return node;
8079  }
8080 
8081  switch (node->node_type)
8082  {
8083  case PT_EXPR:
8084  node = pt_fold_const_expr (parser, node, arg);
8085  break;
8086  case PT_FUNCTION:
8087  node = pt_fold_const_function (parser, node);
8088  break;
8089  default:
8090  break;
8091  }
8092 
8093  if (node == NULL)
8094  {
8095  PT_INTERNAL_ERROR (parser, "pt_fold_constants");
8096  return NULL;
8097  }
8098 
8099  return node;
8100 }
8101 
8102 /*
8103  * pt_eval_type () -
8104  * return:
8105  * parser(in):
8106  * node(in):
8107  * arg(in):
8108  * continue_walk(in):
8109  */
8110 static PT_NODE *
8111 pt_eval_type (PARSER_CONTEXT * parser, PT_NODE * node, void *arg, int *continue_walk)
8112 {
8113  PT_NODE *dt = NULL, *arg1 = NULL, *arg2 = NULL;
8114  PT_NODE *spec = NULL;
8115  SEMANTIC_CHK_INFO *sc_info = (SEMANTIC_CHK_INFO *) arg;
8116  PT_NODE *list;
8117  STATEMENT_SET_FOLD do_fold;
8118  PT_MISC_TYPE is_subquery;
8119 
8120  switch (node->node_type)
8121  {
8122  case PT_EXPR:
8123  node = pt_eval_expr_type (parser, node);
8124  if (node == NULL)
8125  {
8126  assert (false);
8127  PT_INTERNAL_ERROR (parser, "pt_eval_type");
8128  return NULL;
8129  }
8130  break;
8131 
8132  case PT_FUNCTION:
8133  node = pt_eval_function_type (parser, node);
8134  if (node == NULL)
8135  {
8136  assert (false);
8137  PT_INTERNAL_ERROR (parser, "pt_eval_type");
8138  return NULL;
8139  }
8140  break;
8141 
8142  case PT_METHOD_CALL:
8143  node = pt_eval_method_call_type (parser, node);
8144  break;
8145 
8146  case PT_CREATE_INDEX:
8147  node->info.index.where = pt_where_type (parser, node->info.index.where);
8148  break;
8149 
8150  case PT_DELETE:
8151  node->info.delete_.search_cond = pt_where_type (parser, node->info.delete_.search_cond);
8152  break;
8153 
8154  case PT_UPDATE:
8155  node->info.update.search_cond = pt_where_type (parser, node->info.update.search_cond);
8156  break;
8157 
8158  case PT_MERGE:
8159  node->info.merge.search_cond = pt_where_type (parser, node->info.merge.search_cond);
8160  node->info.merge.insert.search_cond = pt_where_type (parser, node->info.merge.insert.search_cond);
8161  node->info.merge.update.search_cond = pt_where_type (parser, node->info.merge.update.search_cond);
8162  node->info.merge.update.del_search_cond = pt_where_type (parser, node->info.merge.update.del_search_cond);
8163 
8164  if (parser->set_host_var == 0)
8165  {
8166  PT_NODE *v;
8167  PT_NODE *list = NULL;
8168  PT_NODE *attr = NULL;
8169  DB_DOMAIN *d;
8170 
8171  /* insert part */
8172  for (list = node->info.merge.insert.value_clauses; list != NULL; list = list->next)
8173  {
8174  attr = node->info.merge.insert.attr_list;
8175  for (v = list->info.node_list.list; v != NULL && attr != NULL; v = v->next, attr = attr->next)
8176  {
8177  if (PT_IS_HOSTVAR (v) && v->expected_domain == NULL)
8178  {
8179  d = pt_node_to_db_domain (parser, attr, NULL);
8180  d = tp_domain_cache (d);
8181  SET_EXPECTED_DOMAIN (v, d);
8183  }
8184  }
8185  }
8186  }
8187  break;
8188 
8189  case PT_SELECT:
8190  if (node->info.query.q.select.list)
8191  {
8192  /* for value query, compatibility check for rows */
8193  if (PT_IS_VALUE_QUERY (node))
8194  {
8196  {
8197  break;
8198  }
8199  list = node->info.query.q.select.list->info.node_list.list;
8200  assert (list != NULL);
8201 
8202  node->type_enum = list->type_enum;
8203  dt = list->data_type;
8204  }
8205  else
8206  {
8207  node->type_enum = node->info.query.q.select.list->type_enum;
8208  dt = node->info.query.q.select.list->data_type;
8209  }
8210 
8211  if (dt)
8212  {
8213  if (node->data_type)
8214  {
8215  parser_free_tree (parser, node->data_type);
8216  }
8217 
8218  node->data_type = parser_copy_tree_list (parser, dt);
8219  }
8220  }
8221 
8222  for (spec = node->info.query.q.select.from; spec; spec = spec->next)
8223  {
8224  if (spec->node_type == PT_SPEC && spec->info.spec.on_cond)
8225  {
8226  spec->info.spec.on_cond = pt_where_type (parser, spec->info.spec.on_cond);
8227  }
8228  }
8229 
8230  node->info.query.q.select.where = pt_where_type (parser, node->info.query.q.select.where);
8231 
8232  is_subquery = node->info.query.is_subquery;
8233 
8234  if (sc_info->donot_fold == false
8235  && (is_subquery == PT_IS_SUBQUERY || is_subquery == PT_IS_UNION_SUBQUERY
8236  || is_subquery == PT_IS_CTE_NON_REC_SUBQUERY || is_subquery == PT_IS_CTE_REC_SUBQUERY)
8237  && pt_false_where (parser, node))
8238  {
8239  node = pt_to_false_subquery (parser, node);
8240  }
8241  else
8242  {
8243  node->info.query.q.select.connect_by = pt_where_type_keep_true (parser, node->info.query.q.select.connect_by);
8244  node->info.query.q.select.start_with = pt_where_type (parser, node->info.query.q.select.start_with);
8245  node->info.query.q.select.after_cb_filter = pt_where_type (parser, node->info.query.q.select.after_cb_filter);
8246  node->info.query.q.select.having = pt_where_type (parser, node->info.query.q.select.having);
8247  node->info.query.orderby_for = pt_where_type (parser, node->info.query.orderby_for);
8248  }
8249  break;
8250 
8251  case PT_DO:
8252  if (node->info.do_.expr)
8253  {
8254  node->type_enum = node->info.do_.expr->type_enum;
8255  dt = node->info.do_.expr->data_type;
8256  if (dt)
8257  {
8258  if (node->data_type)
8259  {
8260  parser_free_tree (parser, node->data_type);
8261  }
8262 
8263  node->data_type = parser_copy_tree_list (parser, dt);
8264  }
8265  }
8266  break;
8267 
8268  case PT_INSERT:
8269  if (node->info.insert.spec)
8270  {
8271  node->type_enum = PT_TYPE_OBJECT;
8273  if (dt)
8274  {
8275  dt->type_enum = PT_TYPE_OBJECT;
8276  node->data_type = dt;
8277  dt->info.data_type.entity = parser_copy_tree (parser, node->info.insert.spec->info.spec.flat_entity_list);
8278  }
8279  }
8280 
8281  if (parser->set_host_var == 0)
8282  {
8283  PT_NODE *v = NULL;
8284  PT_NODE *list = NULL;
8285  PT_NODE *attr = NULL;
8286  DB_DOMAIN *d;
8287 
8288  for (list = node->info.insert.value_clauses; list != NULL; list = list->next)
8289  {
8290  attr = node->info.insert.attr_list;
8291  for (v = list->info.node_list.list; v != NULL && attr != NULL; v = v->next, attr = attr->next)
8292  {
8293  if (PT_IS_HOSTVAR (v) && v->expected_domain == NULL)
8294  {
8295  d = pt_node_to_db_domain (parser, attr, NULL);
8296  d = tp_domain_cache (d);
8297  SET_EXPECTED_DOMAIN (v, d);
8299  }
8300  }
8301  }
8302  }
8303  break;
8304 
8305  case PT_UNION:
8306  case PT_DIFFERENCE:
8307  case PT_INTERSECTION:
8308  /* a PT_CTE node is actually a union between two queries */
8309  case PT_CTE:
8310 
8311  /* check if union can be folded */
8312  do_fold = pt_check_union_is_foldable (parser, node);
8313  if (do_fold != STATEMENT_SET_FOLD_NOTHING)
8314  {
8315  node = pt_fold_union (parser, node, do_fold);
8316  }
8317  else
8318  {
8319  /* check that signatures are compatible */
8320  if (node->node_type == PT_CTE)
8321  {
8322  arg1 = node->info.cte.non_recursive_part;
8323  arg2 = node->info.cte.recursive_part;
8324  if (arg2 != NULL && (pt_false_where (parser, arg1) || pt_false_where (parser, arg2)))
8325  {
8326  /* the recursive_part can be removed if one of the parts has false_where */
8327  parser_free_tree (parser, node->info.cte.recursive_part);
8328  arg2 = node->info.cte.recursive_part = NULL;
8329  }
8330 
8331  if (arg2 == NULL)
8332  {
8333  /* then the CTE is not recursive (just one part) */
8334  break;
8335  }
8336  }
8337  else
8338  {
8339  arg1 = node->info.query.q.union_.arg1;
8340  arg2 = node->info.query.q.union_.arg2;
8341  }
8342 
8343  if ((arg1 && PT_IS_VALUE_QUERY (arg1)) || (arg2 && PT_IS_VALUE_QUERY (arg2)))
8344  {
8346  {
8347  break;
8348  }
8349  }
8350  else
8351  {
8353  {
8354  break;
8355  }
8356  }
8357 
8358  node->type_enum = arg1->type_enum;
8359  dt = arg1->data_type;
8360  if (dt)
8361  {
8362  node->data_type = parser_copy_tree_list (parser, dt);
8363  }
8364  }
8365  break;
8366 
8367  case PT_VALUE:
8368  case PT_NAME:
8369  case PT_DOT_:
8370  /* these cases have types already assigned to them by parser and semantic name resolution. */
8371  break;
8372 
8373  case PT_HOST_VAR:
8374  if (node->type_enum == PT_TYPE_NONE && node->info.host_var.var_type == PT_HOST_IN)
8375  {
8376  /* type is not known yet (i.e, compile before bind a value) */
8377  node->type_enum = PT_TYPE_MAYBE;
8378  }
8379  break;
8380  case PT_SET_OPT_LVL:
8381  case PT_GET_OPT_LVL:
8382  node = pt_eval_opt_type (parser, node);
8383  break;
8384 
8385  default:
8386  break;
8387  }
8388 
8389  return node;
8390 }
8391 
8392 /*
8393  * pt_chop_to_one_select_item () -
8394  * return: none
8395  * parser(in):
8396  * node(in/out): an EXISTS subquery
8397  */
8398 static void
8399 pt_chop_to_one_select_item (PARSER_CONTEXT * parser, PT_NODE * node)
8400 {
8401  if (pt_is_query (node))
8402  {
8403  if (node->node_type == PT_SELECT)
8404  {
8405  /* chop to one select item */
8406  if (node->info.query.q.select.list && node->info.query.q.select.list->next)
8407  {
8408  parser_free_tree (parser, node->info.query.q.select.list->next);
8409  node->info.query.q.select.list->next = NULL;
8410  }
8411  }
8412  else
8413  {
8414  pt_chop_to_one_select_item (parser, node->info.query.q.union_.arg1);
8415  pt_chop_to_one_select_item (parser, node->info.query.q.union_.arg2);
8416  }
8417 
8418  /* remove unneeded order by */
8419  if (node->info.query.order_by && !node->info.query.q.select.connect_by)
8420  {
8421  parser_free_tree (parser, node->info.query.order_by);
8422  node->info.query.order_by = NULL;
8423  }
8424  }
8425 }
8426 
8427 /*
8428  * pt_append_query_select_list () - append to the query's lists attrs
8429  *
8430  * result: query with all lists updated
8431  * parser(in):
8432  * query(in):
8433  * attrs(in): list of attributes to append to the query
8434  */
8435 PT_NODE *
8437 {
8438  if (!attrs)
8439  {
8440  return query;
8441  }
8442 
8443  switch (query->node_type)
8444  {
8445  case PT_SELECT:
8446  {
8447  PT_NODE *select_list = query->info.query.q.select.list;
8448 
8449  query->info.query.q.select.list = parser_append_node (attrs, select_list);
8450  break;
8451  }
8452  case PT_DIFFERENCE:
8453  case PT_INTERSECTION:
8454  case PT_UNION:
8457  break;
8458  default:
8459  break;
8460  }
8461 
8462  return query;
8463 }
8464 
8465 /*
8466  * pt_wrap_select_list_with_cast_op () - cast the nodes of a select list
8467  * to the type new_type, using the specified
8468  * precision and scale
8469  * return : NO_ERROR on success, error code on failure
8470  * parser(in) : parser context
8471  * query(in) : the select query
8472  * new_type(in) : the new_type
8473  * p(in) : precision
8474  * s(in) : scale
8475  * data_type(in) : the data type of new_type
8476  * force_wrap(in): forces wrapping with cast for collatable nodes
8477  */
8478 int
8480  PT_NODE * data_type, bool force_wrap)
8481 {
8482  switch (query->node_type)
8483  {
8484  case PT_SELECT:
8485  {
8486  PT_NODE *item = NULL;
8487  PT_NODE *prev = NULL;
8488  PT_NODE *new_node = NULL;
8489  PT_NODE *select_list = NULL;
8490  PT_NODE *node_list = NULL;
8491 
8492  /* values query's select_list is pt_node_list-->pt_node_list-->... */
8493  if (PT_IS_VALUE_QUERY (query))
8494  {
8495  for (node_list = query->info.query.q.select.list; node_list != NULL; node_list = node_list->next)
8496  {
8497  select_list = node_list->info.node_list.list;
8498 
8499  prev = NULL;
8500  for (item = select_list; item != NULL; prev = item, item = item->next)
8501  {
8502  new_node = NULL;
8503  if (item->type_enum == new_type && !force_wrap)
8504  {
8505  continue;
8506  }
8507 
8508  new_node = pt_wrap_with_cast_op (parser, item, new_type, p, s, data_type);
8509  if (new_node == NULL)
8510  {
8511  return ER_FAILED;
8512  }
8513 
8514  if (new_node != item)
8515  {
8516  item = new_node;
8517  PT_SET_VALUE_QUERY (item);
8518  /* first node in the list */
8519  if (prev == NULL)
8520  {
8521  node_list->info.node_list.list = item;
8522  }
8523  else
8524  {
8525  prev->next = item;
8526  }
8527  }
8528  }
8529  }
8530  }
8531  else
8532  {
8533  select_list = pt_get_select_list (parser, query);
8534 
8535  for (item = select_list; item != NULL; prev = item, item = item->next)
8536  {
8537  if (item->is_hidden_column)
8538  {
8539  continue;
8540  }
8541  new_node = NULL;
8542  if (item->type_enum == new_type && !force_wrap)
8543  {
8544  continue;
8545  }
8546  new_node = pt_wrap_with_cast_op (parser, item, new_type, p, s, data_type);
8547  if (new_node == NULL)
8548  {
8549  return ER_FAILED;
8550  }
8551 
8552  if (new_node != item)
8553  {
8554  item = new_node;
8555  /* first node in the list */
8556  if (prev == NULL)
8557  {
8558  query->info.query.q.select.list = item;
8559  }
8560  else
8561  {
8562  prev->next = item;
8563  }
8564  }
8565  }
8566  }
8567  break;
8568  }
8569  case PT_DIFFERENCE:
8570  case PT_INTERSECTION:
8571  case PT_UNION:
8572  {
8573  int err = NO_ERROR;
8574  /* wrap with cast union select values for queries arg1 and arg2 */
8575  err =
8576  pt_wrap_select_list_with_cast_op (parser, query->info.query.q.union_.arg1, new_type, p, s, data_type,
8577  force_wrap);
8578  if (err != NO_ERROR)
8579  {
8580  return err;
8581  }
8582 
8583  err =
8584  pt_wrap_select_list_with_cast_op (parser, query->info.query.q.union_.arg2, new_type, p, s, data_type,
8585  force_wrap);
8586  if (err != NO_ERROR)
8587  {
8588  return err;
8589  }
8590 
8591  break;
8592  }
8593  default:
8594  return 0;
8595  break;
8596  }
8597  return NO_ERROR;
8598 }
8599 
8600 /*
8601  * pt_wrap_collection_with_cast_op () - wrap a node with a cast to a
8602  * collection type
8603  *
8604  * return: the wrapped node or null on error
8605  * parser(in) : parser context
8606  * arg(in/out) : the node to be wrapped
8607  * set_type(in) : the collection type
8608  * data_type(in): the type of the elements of the collection
8609  * p(in) : the precision of the collection elements
8610  * s(in) : the scale of the collection elements
8611  */
8612 
8613 PT_NODE *
8615  bool for_collation)
8616 {
8617  PT_NODE *new_att, *set_dt, *next_att;
8618 
8619  if (arg->node_type == PT_FUNCTION && set_data->next == NULL)
8620  {
8621  /* if the argument is function and domain to cast has only one type */
8622  switch (arg->info.function.function_type)
8623  {
8624  case F_SET:
8625  case F_MULTISET:
8626  case F_SEQUENCE:
8627  {
8628  PT_NODE **first = &arg->info.function.arg_list, *arg_list = *first, *prev = NULL;
8629  bool is_numeric = PT_IS_NUMERIC_TYPE (set_data->type_enum);
8630 
8631  /* walk through set members and cast them to set_data->type_enum if needed */
8632  while (arg_list)
8633  {
8634  next_att = arg_list->next;
8635 
8636  if ((set_data->type_enum != arg_list->type_enum
8637  && (!is_numeric || !PT_IS_NUMERIC_TYPE (arg_list->type_enum)))
8638  || (for_collation == true && PT_HAS_COLLATION (set_data->type_enum)
8639  && PT_HAS_COLLATION (arg_list->type_enum) && arg_list->data_type != NULL
8640  && set_data->info.data_type.collation_id != arg_list->data_type->info.data_type.collation_id))
8641  {
8642  /* Set the expected domain of host variable to type set_data so that at runtime the host variable
8643  * should be casted to it if needed */
8644  if (arg_list->type_enum == PT_TYPE_MAYBE && arg_list->node_type == PT_HOST_VAR)
8645  {
8646  if (for_collation == false)
8647  {
8648  arg_list->expected_domain = pt_data_type_to_db_domain (parser, set_data, NULL);
8649  pt_preset_hostvar (parser, arg_list);
8650  }
8651  }
8652  else
8653  {
8654  new_att = pt_wrap_with_cast_op (parser, arg_list, set_data->type_enum, 0, 0, set_data);
8655  if (!new_att)
8656  {
8658  return NULL;
8659  }
8660 
8662  {
8663  assert (new_att->node_type == PT_EXPR);
8665  }
8666 
8667  if (prev)
8668  {
8669  prev->next = new_att;
8670  new_att->next = next_att;
8671  }
8672  else
8673  {
8674  *first = new_att;
8675  }
8676  arg_list = new_att;
8677  }
8678  }
8679  prev = arg_list;
8680  arg_list = next_att;
8681  }
8682  }
8683  return arg;
8684  default:
8685  break;
8686  }
8687 
8688  }
8689 
8690  next_att = arg->next;
8691  arg->next = NULL;
8692  new_att = parser_new_node (parser, PT_EXPR);
8693  set_dt = parser_new_node (parser, PT_DATA_TYPE);
8694 
8695  if (!new_att || !set_dt)
8696  {
8698  return NULL;
8699  }
8700 
8701  /* move alias */
8702  new_att->line_number = arg->line_number;
8703  new_att->column_number = arg->column_number;
8704  new_att->alias_print = arg->alias_print;
8705  new_att->is_hidden_column = arg->is_hidden_column;
8706  arg->alias_print = NULL;
8707 
8708 
8709  /* set the data type of the collection */
8710  set_dt->type_enum = set_type;
8711  set_dt->data_type = parser_copy_tree_list (parser, set_data);
8712 
8713  new_att->type_enum = set_type;
8714  new_att->info.expr.op = PT_CAST;
8715  new_att->info.expr.cast_type = set_dt;
8716  new_att->info.expr.arg1 = arg;
8717  new_att->next = next_att;
8718 
8719  new_att->data_type = set_data;
8721  return new_att;
8722 }
8723 
8724 /*
8725  * pt_wrap_with_cast_op () -
8726  * return:
8727  * parser(in):
8728  * arg(in/out):
8729  * new_type(in):
8730  * p(in):
8731  * s(in):
8732  * desired_dt(in):
8733  */
8734 PT_NODE *
8735 pt_wrap_with_cast_op (PARSER_CONTEXT * parser, PT_NODE * arg, PT_TYPE_ENUM new_type, int p, int s, PT_NODE * desired_dt)
8736 {
8737  PT_NODE *new_att, *new_dt, *next_att;
8738 
8739  assert (arg != NULL);
8740  if (arg == NULL)
8741  {
8742  return NULL;
8743  }
8744 
8745  next_att = arg->next;
8746  arg->next = NULL;
8747  new_att = parser_new_node (parser, PT_EXPR);
8748  if (new_att == NULL)
8749  {
8750  return NULL;
8751  }
8752 
8753  if (PT_IS_COLLECTION_TYPE (new_type))
8754  {
8755  new_dt = parser_new_node (parser, PT_DATA_TYPE);
8756  if (new_dt == NULL)
8757  {
8758  return NULL;
8759  }
8760  new_dt->type_enum = new_type;
8761  new_dt->info.data_type.precision = p;
8762  new_dt->info.data_type.dec_precision = s;
8763 
8764  if (desired_dt != NULL && !PT_IS_COLLECTION_TYPE (desired_dt->type_enum))
8765  {
8766  /* desired_dt contains a list of types of elements from the collection */
8767  new_dt->data_type = parser_copy_tree_list (parser, desired_dt);
8768  }
8769  /* If desired_dt is not null but is a collection type, we can't actually make a decision here because we can't
8770  * make a distinction between collection of collections and simple collections. In the case of collection of
8771  * collections, the correct type of each element will be set when the collection is validated in the context in
8772  * which it is used */
8773  }
8774  else if (desired_dt == NULL)
8775  {
8776  new_dt = parser_new_node (parser, PT_DATA_TYPE);
8777  if (new_dt == NULL)
8778  {
8779  return NULL;
8780  }
8781  new_dt->type_enum = new_type;
8782  new_dt->info.data_type.precision = p;
8783  new_dt->info.data_type.dec_precision = s;
8784  if (arg->data_type != NULL && PT_HAS_COLLATION (arg->data_type->type_enum))
8785  {
8786  new_dt->info.data_type.units = arg->data_type->info.data_type.units;
8787  new_dt->info.data_type.collation_id = arg->data_type->info.data_type.collation_id;
8788  }
8789 
8790  if (PT_HAS_COLLATION (new_type) && arg->type_enum == PT_TYPE_MAYBE)
8791  {
8792  /* when wrapping a TYPE MAYBE, we don't change the collation */
8796  }
8797  }
8798  else
8799  {
8800  new_dt = parser_copy_tree_list (parser, desired_dt);
8801  }
8802 
8803  /* move alias */
8804  new_att->line_number = arg->line_number;
8805  new_att->column_number = arg->column_number;
8806  new_att->alias_print = arg->alias_print;
8807  new_att->is_hidden_column = arg->is_hidden_column;
8808  arg->alias_print = NULL;
8809 
8810  new_att->type_enum = new_type;
8811  new_att->info.expr.op = PT_CAST;
8812  new_att->info.expr.cast_type = new_dt;
8813  new_att->info.expr.arg1 = arg;
8814  new_att->next = next_att;
8815 
8816  new_att->data_type = parser_copy_tree_list (parser, new_dt);
8818 
8819  return new_att;
8820 }
8821 
8822 /*
8823  * pt_preset_hostvar () -
8824  * return: none
8825  * parser(in):
8826  * hv_node(in):
8827  */
8828 void
8830 {
8831  pt_hv_consistent_data_type_with_domain (parser, hv_node);
8832  if (parser->host_var_count <= hv_node->info.host_var.index)
8833  {
8834  /* automated parameters are not needed an expected domain */
8835  return;
8836  }
8837 
8838  parser->host_var_expected_domains[hv_node->info.host_var.index] = hv_node->expected_domain;
8839 }
8840 
8841 /* pt_set_expected_domain - set the expected tomain of a PT_NODE
8842  * return: void
8843  * node (in/out) : the node to wich to set the expected domain
8844  * domain (in) : the expected domain
8845  */
8846 void
8848 {
8849  PT_NODE *_or_next = NULL;
8850  node->expected_domain = domain;
8851  _or_next = node->or_next;
8852  while (_or_next)
8853  {
8854  if (_or_next->type_enum == PT_TYPE_MAYBE && _or_next->expected_domain == NULL)
8855  {
8856  _or_next->expected_domain = domain;
8857  }
8858  _or_next = _or_next->or_next;
8859  }
8860 }
8861 
8862 /*
8863  * pt_is_able_to_determine_return_type () -
8864  * return: true if the type of the return value can be determined
8865  * regardless of its arguments, otherwise false.
8866  * op(in):
8867  */
8868 static bool
8869 pt_is_able_to_determine_return_type (const PT_OP_TYPE op)
8870 {
8871  switch (op)
8872  {
8873  case PT_CAST:
8874  case PT_TO_NUMBER:
8875  case PT_TO_CHAR:
8876  case PT_TO_DATE:
8877  case PT_TO_TIME:
8878  case PT_TO_TIMESTAMP:
8879  case PT_TO_DATETIME:
8880  case PT_POSITION:
8881  case PT_FINDINSET:
8882  case PT_OCTET_LENGTH:
8883  case PT_BIT_LENGTH:
8884  case PT_CHAR_LENGTH:
8885  case PT_TIME_FORMAT:
8886  case PT_TIMESTAMP:
8887  case PT_UNIX_TIMESTAMP:
8888  case PT_SIGN:
8889  case PT_CHR:
8890  case PT_ADD_MONTHS:
8891  case PT_LAST_DAY:
8892  case PT_MONTHS_BETWEEN:
8893  case PT_DATE_ADD:
8894  case PT_ADDDATE:
8895  case PT_FORMAT:
8896  case PT_DATE_SUB:
8897  case PT_SUBDATE:
8898  case PT_DATE_FORMAT:
8899  case PT_STR_TO_DATE:
8900  case PT_SIN:
8901  case PT_COS:
8902  case PT_TAN:
8903  case PT_ASIN:
8904  case PT_ACOS:
8905  case PT_ATAN:
8906  case PT_ATAN2:
8907  case PT_COT:
8908  case PT_LOG:
8909  case PT_EXP:
8910  case PT_SQRT:
8911  case PT_DEGREES:
8912  case PT_LN:
8913  case PT_LOG2:
8914  case PT_LOG10:
8915  case PT_POWER:
8916  case PT_FIELD:
8917  case PT_LOCATE:
8918  case PT_STRCMP:
8919  case PT_RADIANS:
8920  case PT_BIT_AND:
8921  case PT_BIT_OR:
8922  case PT_BIT_XOR:
8923  case PT_BIT_NOT:
8924  case PT_BITSHIFT_LEFT:
8925  case PT_BITSHIFT_RIGHT:
8926  case PT_BIT_COUNT:
8927  case PT_DATEDIFF:
8928  case PT_TIMEDIFF:
8929  case PT_DATEF:
8930  case PT_TIMEF:
8931  case PT_ISNULL:
8932  case PT_RAND:
8933  case PT_DRAND:
8934  case PT_RANDOM:
8935  case PT_DRANDOM:
8936  case PT_BIT_TO_BLOB:
8937  case PT_BLOB_FROM_FILE:
8938  case PT_BLOB_LENGTH:
8939  case PT_BLOB_TO_BIT:
8940  case PT_CHAR_TO_BLOB:
8941  case PT_CHAR_TO_CLOB:
8942  case PT_CLOB_FROM_FILE:
8943  case PT_CLOB_LENGTH:
8944  case PT_CLOB_TO_CHAR:
8945  case PT_TYPEOF:
8946  case PT_YEARF:
8947  case PT_MONTHF:
8948  case PT_DAYF:
8949  case PT_DAYOFMONTH:
8950  case PT_HOURF:
8951  case PT_MINUTEF:
8952  case PT_SECONDF:
8953  case PT_QUARTERF:
8954  case PT_WEEKDAY:
8955  case PT_DAYOFWEEK:
8956  case PT_DAYOFYEAR:
8957  case PT_TODAYS:
8958  case PT_FROMDAYS:
8959  case PT_TIMETOSEC:
8960  case PT_SECTOTIME:
8961  case PT_WEEKF:
8962  case PT_MAKEDATE:
8963  case PT_MAKETIME:
8964  case PT_BIN:
8965  case PT_CASE:
8966  case PT_DECODE:
8967  case PT_LIKE:
8968  case PT_NOT_LIKE:
8969  case PT_RLIKE:
8970  case PT_NOT_RLIKE:
8971  case PT_RLIKE_BINARY:
8972  case PT_NOT_RLIKE_BINARY:
8973  case PT_EVALUATE_VARIABLE:
8974  case PT_DEFINE_VARIABLE:
8975  case PT_HEX:
8976  case PT_ASCII:
8977  case PT_CONV:
8979  case PT_INET_ATON:
8980  case PT_INET_NTOA:
8981  case PT_CHARSET:
8982  case PT_COERCIBILITY:
8983  case PT_COLLATION:
8984  case PT_WIDTH_BUCKET:
8985  case PT_AES_ENCRYPT:
8986  case PT_AES_DECRYPT:
8987  case PT_SHA_ONE:
8988  case PT_SHA_TWO:
8989  case PT_SLEEP:
8990  case PT_TO_DATETIME_TZ:
8991  case PT_TO_TIMESTAMP_TZ:
8992  case PT_CRC32:
8993  case PT_DISK_SIZE:
8994  case PT_SCHEMA_DEF:
8995  return true;
8996 
8997  default:
8998  return false;
8999  }
9000 }
9001 
9002 /*
9003  * pt_get_common_datetime_type () -
9004  * return:
9005  * common_type(in):
9006  * arg1_type(in):
9007  * arg2_type(in):
9008  * arg1(in):
9009  * arg1(in):
9010  */
9011 static PT_TYPE_ENUM
9012 pt_get_common_datetime_type (PARSER_CONTEXT * parser, PT_TYPE_ENUM common_type, PT_TYPE_ENUM arg1_type,
9013  PT_TYPE_ENUM arg2_type, PT_NODE * arg1, PT_NODE * arg2)
9014 {
9015  PT_TYPE_ENUM arg_type, arg_base_type;
9016  PT_NODE *arg_ptr = NULL;
9017  PT_NODE *arg_base = NULL;
9018 
9019  assert (arg1_type != arg2_type);
9020 
9021  if (arg1_type == common_type)
9022  {
9023  arg_base_type = arg1_type;
9024  arg_base = arg1;
9025  arg_type = arg2_type;
9026  arg_ptr = arg2;
9027  }
9028  else
9029  {
9030  arg_base_type = arg2_type;
9031  arg_base = arg2;
9032  arg_type = arg1_type;
9033  arg_ptr = arg1;
9034  }
9035 
9036  if (PT_IS_CHAR_STRING_TYPE (arg_type))
9037  {
9038  if (pt_coerce_str_to_time_date_utime_datetime (parser, arg_ptr, &arg_type) == NO_ERROR)
9039  {
9040  PT_TYPE_ENUM result_type = pt_common_type (arg_base_type, arg_type);
9041 
9042  if (arg_type != arg_base_type)
9043  {
9044  return pt_get_common_datetime_type (parser, result_type, arg_base_type, arg_type, arg_base, arg_ptr);
9045  }
9046 
9047  return result_type;
9048  }
9049  }
9050  else if (PT_IS_DATE_TIME_TYPE (arg_type))
9051  {
9052  if (pt_coerce_value (parser, arg_ptr, arg_ptr, common_type, NULL) == NO_ERROR)
9053  {
9054  return common_type;
9055  }
9056  }
9057  else if (PT_IS_NUMERIC_TYPE (arg_type))
9058  {
9059  return common_type;
9060  }
9061 
9062  return PT_TYPE_NONE;
9063 }
9064 
9065 /*
9066  * pt_eval_expr_type () -
9067  * return:
9068  * parser(in):
9069  * node(in):
9070  */
9071 static PT_NODE *
9072 pt_eval_expr_type (PARSER_CONTEXT * parser, PT_NODE * node)
9073 {
9074  PT_OP_TYPE op;
9075  PT_NODE *arg1 = NULL, *arg2 = NULL, *arg3 = NULL;
9076  PT_NODE *arg1_hv = NULL, *arg2_hv = NULL, *arg3_hv = NULL;
9077  PT_TYPE_ENUM arg1_type = PT_TYPE_NONE, arg2_type = PT_TYPE_NONE;
9078  PT_TYPE_ENUM arg3_type = PT_TYPE_NONE, common_type = PT_TYPE_NONE;
9079  TP_DOMAIN *d;
9080  PT_NODE *cast_type;
9081  PT_NODE *new_att;
9082  PT_TYPE_ENUM new_type;
9083  int first_node;
9084  PT_NODE *expr = NULL;
9085  bool check_expr_coll = true;
9086  DB_TYPE dbtype2 = DB_TYPE_UNKNOWN, dbtype3 = DB_TYPE_UNKNOWN;
9087 
9088  /* by the time we get here, the leaves have already been typed. this is because this function is called from a post
9089  * function of a parser_walk_tree, after all leaves have been visited. */
9090 
9091  op = node->info.expr.op;
9092  if (pt_is_enumeration_special_comparison (node->info.expr.arg1, op, node->info.expr.arg2))
9093  {
9094  /* handle special cases for the enumeration type */
9095  node = pt_fix_enumeration_comparison (parser, node);
9096  if (node == NULL)
9097  {
9098  return NULL;
9099  }
9100  op = node->info.expr.op;
9101  if (pt_has_error (parser))
9102  {
9103  goto error;
9104  }
9105  }
9106  /* shortcut for FUNCTION HOLDER */
9107  if (op == PT_FUNCTION_HOLDER)
9108  {
9109  PT_NODE *func = NULL;
9110  /* this may be a 2nd pass, tree may be already const folded */
9111  if (node->info.expr.arg1->node_type == PT_FUNCTION)
9112  {
9113  func = pt_eval_function_type (parser, node->info.expr.arg1);
9114  node->type_enum = func->type_enum;
9115  if (node->data_type == NULL && func->data_type != NULL)
9116  {
9117  node->data_type = parser_copy_tree (parser, func->data_type);
9118  }
9119  }
9120  else
9121  {
9122  assert (node->info.expr.arg1->node_type == PT_VALUE);
9123  }
9124  return node;
9125  }
9126 
9127 
9128  arg1 = node->info.expr.arg1;
9129  if (arg1)
9130  {
9131  arg1_type = arg1->type_enum;
9132 
9133  if (arg1->node_type == PT_HOST_VAR && arg1->type_enum == PT_TYPE_MAYBE)
9134  {
9135  arg1_hv = arg1;
9136  }
9137 
9138  /* Special case handling for unary operators on host variables (-?) or (prior ?) or (connect_by_root ?) */
9139  if (arg1->node_type == PT_EXPR
9140  && (arg1->info.expr.op == PT_UNARY_MINUS || arg1->info.expr.op == PT_PRIOR
9141  || arg1->info.expr.op == PT_CONNECT_BY_ROOT || arg1->info.expr.op == PT_QPRIOR
9142  || arg1->info.expr.op == PT_BIT_NOT || arg1->info.expr.op == PT_BIT_COUNT)
9143  && arg1->type_enum == PT_TYPE_MAYBE && arg1->info.expr.arg1->node_type == PT_HOST_VAR
9144  && arg1->info.expr.arg1->type_enum == PT_TYPE_MAYBE)
9145  {
9146  arg1_hv = arg1->info.expr.arg1;
9147  }
9148  }
9149 
9150  arg2 = node->info.expr.arg2;
9151  if (arg2)
9152  {
9153  if (arg2->or_next == NULL)
9154  {
9155  arg2_type = arg2->type_enum;
9156  }
9157  else
9158  {
9159  PT_NODE *temp;
9160  PT_TYPE_ENUM temp_type;
9161 
9162  common_type = PT_TYPE_NONE;
9163  /* do traverse multi-args in RANGE operator */
9164  for (temp = arg2; temp; temp = temp->or_next)
9165  {
9166  temp_type = pt_common_type (arg1_type, temp->type_enum);
9167  if (temp_type != PT_TYPE_NONE)
9168  {
9169  common_type = (common_type == PT_TYPE_NONE) ? temp_type : pt_common_type (common_type, temp_type);
9170  }
9171  }
9172  arg2_type = common_type;
9173  }
9174 
9175  if (arg2->node_type == PT_HOST_VAR && arg2->type_enum == PT_TYPE_MAYBE)
9176  {
9177  arg2_hv = arg2;
9178  }
9179 
9180  /* Special case handling for unary operators on host variables (-?) or (prior ?) or (connect_by_root ?) */
9181  if (arg2->node_type == PT_EXPR
9182  && (arg2->info.expr.op == PT_UNARY_MINUS || arg2->info.expr.op == PT_PRIOR
9183  || arg2->info.expr.op == PT_CONNECT_BY_ROOT || arg2->info.expr.op == PT_QPRIOR
9184  || arg2->info.expr.op == PT_BIT_NOT || arg2->info.expr.op == PT_BIT_COUNT)
9185  && arg2->type_enum == PT_TYPE_MAYBE && arg2->info.expr.arg1->node_type == PT_HOST_VAR
9186  && arg2->info.expr.arg1->type_enum == PT_TYPE_MAYBE)
9187  {
9188  arg2_hv = arg2->info.expr.arg1;
9189  }
9190  }
9191 
9192  arg3 = node->info.expr.arg3;
9193  if (arg3)
9194  {
9195  arg3_type = arg3->type_enum;
9196  if (arg3->node_type == PT_HOST_VAR && arg3->type_enum == PT_TYPE_MAYBE)
9197  {
9198  arg3_hv = arg3;
9199  }
9200  }
9201 
9202  /*
9203  * At this point, arg1_hv is non-NULL (and equal to arg1) if it represents
9204  * a dynamic host variable, i.e., a host var parameter that hasn't had
9205  * a value supplied at compile time. Same for arg2_hv and arg3_hv...
9206  */
9207  common_type = arg1_type;
9208  expr = node;
9209 
9210  /* adjust expression definition to fit the signature implementation */
9211  switch (op)
9212  {
9213  case PT_PLUS:
9214  if (arg1_type == PT_TYPE_NULL || arg2_type == PT_TYPE_NULL)
9215  {
9217  || (!PT_IS_STRING_TYPE (arg1_type) && !PT_IS_STRING_TYPE (arg2_type)))
9218  {
9219  node->type_enum = PT_TYPE_NULL;
9220  goto error;
9221  }
9222  }
9223  if (arg1_type == PT_TYPE_MAYBE || arg2_type == PT_TYPE_MAYBE)
9224  {
9225  node->type_enum = PT_TYPE_MAYBE;
9226  goto cannot_use_signature;
9227  }
9229  {
9230  /* in mysql mode, PT_PLUS is not defined on date and number */
9231  break;
9232  }
9233  /* PT_PLUS has four overloads for which we cannot apply symmetric rule 1. DATE/TIME type + NUMBER 2. NUMBER +
9234  * DATE/TIME type 3. DATE/TIME type + STRING 4. STRING + DATE/TIME type STRING/NUMBER operand involved is
9235  * coerced to BIGINT. For these overloads, PT_PLUS is a syntactic sugar for the ADD_DATE expression. Even
9236  * though both PLUS and MINUS have this behavior, we cannot treat them in the same place because, for this
9237  * case, PT_PLUS is commutative and PT_MINUS isn't */
9238  if (PT_IS_DATE_TIME_TYPE (arg1_type)
9239  && (PT_IS_NUMERIC_TYPE (arg2_type) || PT_IS_CHAR_STRING_TYPE (arg2_type) || arg2_type == PT_TYPE_ENUMERATION
9240  || arg2_type == PT_TYPE_MAYBE))
9241  {
9242  if (!PT_IS_DISCRETE_NUMBER_TYPE (arg2_type))
9243  {
9244  /* coerce first argument to BIGINT */
9245  int err = pt_coerce_expression_argument (parser, node, &arg2, PT_TYPE_BIGINT, NULL);
9246  if (err != NO_ERROR)
9247  {
9248  node->type_enum = PT_TYPE_NONE;
9249  goto error;
9250  }
9251  }
9252  node->info.expr.arg2 = arg2;
9253  node->type_enum = arg1_type;
9254  goto error;
9255  }
9256  if (PT_IS_DATE_TIME_TYPE (arg2_type)
9257  && (PT_IS_NUMERIC_TYPE (arg1_type) || PT_IS_CHAR_STRING_TYPE (arg1_type) || arg1_type == PT_TYPE_ENUMERATION
9258  || arg1_type == PT_TYPE_MAYBE))
9259  {
9260  if (!PT_IS_DISCRETE_NUMBER_TYPE (arg1_type))
9261  {
9262  int err = pt_coerce_expression_argument (parser, node, &arg1, PT_TYPE_BIGINT, NULL);
9263  if (err != NO_ERROR)
9264  {
9265  node->type_enum = PT_TYPE_NONE;
9266  goto error;
9267  }
9268  }
9269  node->info.expr.arg1 = arg1;
9270  node->type_enum = arg2_type;
9271  goto error;
9272  }
9273  break;
9274  case PT_MINUS:
9275  if (arg1_type == PT_TYPE_NULL || arg2_type == PT_TYPE_NULL)
9276  {
9277  node->type_enum = PT_TYPE_NULL;
9278  goto error;
9279  }
9280  if (arg1_type == PT_TYPE_MAYBE || arg2_type == PT_TYPE_MAYBE)
9281  {
9282  node->type_enum = PT_TYPE_MAYBE;
9283  goto cannot_use_signature;
9284  }
9286  {
9287  /* in mysql mode - does is not defined on date and number */
9288  break;
9289  }
9290  if (PT_IS_DATE_TIME_TYPE (arg1_type) && (PT_IS_NUMERIC_TYPE (arg2_type) || arg2_type == PT_TYPE_ENUMERATION))
9291  {
9292  if (!PT_IS_DISCRETE_NUMBER_TYPE (arg2_type))
9293  {
9294  /* coerce arg2 to bigint */
9295  int err = pt_coerce_expression_argument (parser, expr, &arg2, PT_TYPE_BIGINT, NULL);
9296  if (err != NO_ERROR)
9297  {
9298  node->type_enum = PT_TYPE_NONE;
9299  goto error;
9300  }
9301  node->info.expr.arg2 = arg2;
9302  }
9303  node->type_enum = arg1_type;
9304  goto error;
9305  }
9306  break;
9307  case PT_BETWEEN_AND:
9308  case PT_BETWEEN_GE_LE:
9309  case PT_BETWEEN_GE_LT:
9310  case PT_BETWEEN_GT_LE:
9311  case PT_BETWEEN_GT_LT:
9312  /* these expressions will be handled by PT_BETWEEN */
9313  node->type_enum = pt_common_type (arg1_type, arg2_type);
9314  goto error;
9315  break;
9316  case PT_BETWEEN:
9317  case PT_NOT_BETWEEN:
9318  /* between and range operators are written like: PT_BETWEEN(arg1, PT_BETWEEN_AND(arg2,arg3)) We convert it to
9319  * PT_BETWEEN(arg1, arg2, arg2) to be able to decide the correct common type of all arguments and we will
9320  * convert it back once we apply the correct casts */
9321  if (arg2->node_type == PT_EXPR && pt_is_between_range_op (arg2->info.expr.op))
9322  {
9323  arg2 = node->info.expr.arg2;
9324  node->info.expr.arg2 = arg2->info.expr.arg1;
9325  node->info.expr.arg3 = arg2->info.expr.arg2;
9326  }
9327  break;
9328  case PT_LIKE:
9329  case PT_NOT_LIKE:
9330  /* [NOT] LIKE operators with an escape clause are parsed like PT_LIKE(arg1, PT_LIKE_ESCAPE(arg2, arg3)). We
9331  * convert it to PT_LIKE(arg1, arg2, arg3) to be able to decide the correct common type of all arguments and we
9332  * will convert it back once we apply the correct casts.
9333  *
9334  * A better approach would be to modify the parser to output PT_LIKE(arg1, arg2, arg3) directly. */
9335 
9336  if (arg2->node_type == PT_EXPR && arg2->info.expr.op == PT_LIKE_ESCAPE)
9337  {
9338  arg2 = node->info.expr.arg2;
9339  node->info.expr.arg2 = arg2->info.expr.arg1;
9340  node->info.expr.arg3 = arg2->info.expr.arg2;
9341  }
9342  break;
9343 
9344  case PT_LIKE_LOWER_BOUND:
9345  case PT_LIKE_UPPER_BOUND:
9346  /* Check if arguments have been handled by PT_LIKE and only the result type needs to be set */
9347  if (arg1->type_enum == PT_TYPE_MAYBE && arg1->expected_domain)
9348  {
9349  node->type_enum = pt_db_to_type_enum (TP_DOMAIN_TYPE (arg1->expected_domain));
9350  goto error;
9351  }
9352  break;
9353 
9354  case PT_IS_IN:
9355  case PT_IS_NOT_IN:
9356  if (arg2->node_type == PT_VALUE)
9357  {
9358  if (PT_IS_COLLECTION_TYPE (arg2->type_enum) && arg2->info.value.data_value.set
9359  && arg2->info.value.data_value.set->next == NULL)
9360  {
9361  /* only one element in set. convert expr as EQ/NE expr. */
9362  PT_NODE *new_arg2;
9363 
9364  new_arg2 = arg2->info.value.data_value.set;
9365 
9366  /* free arg2 */
9367  arg2->info.value.data_value.set = NULL;
9368  parser_free_tree (parser, node->info.expr.arg2);
9369 
9370  /* rewrite arg2 */
9371  node->info.expr.arg2 = new_arg2;
9372  node->info.expr.op = (op == PT_IS_IN) ? PT_EQ : PT_NE;
9373  }
9374  else if (PT_IS_NULL_NODE (arg2))
9375  {
9376  return node;
9377  }
9378  }
9379  break;
9380 
9381  case PT_TO_CHAR:
9382  if (PT_IS_CHAR_STRING_TYPE (arg1_type))
9383  {
9384  arg1->line_number = node->line_number;
9385  arg1->column_number = node->column_number;
9386  arg1->alias_print = node->alias_print;
9387  node->alias_print = NULL;
9388  arg1->next = node->next;
9389  node->next = NULL;
9390  if (arg1->node_type == PT_EXPR)
9391  {
9392  arg1->info.expr.location = node->info.expr.location;
9393  }
9394  else if (arg1->node_type == PT_VALUE)
9395  {
9396  arg1->info.value.location = node->info.expr.location;
9397  }
9398  node->info.expr.arg1 = NULL;
9400 
9401  node = parser_copy_tree_list (parser, arg1);
9402  parser_free_node (parser, arg1);
9403 
9404  return node;
9405  }
9406  else if (PT_IS_NUMERIC_TYPE (arg1_type))
9407  {
9408  bool has_user_format = false;
9409  bool has_user_lang = false;
9410  const char *lang_str;
9411 
9412  assert (arg3 != NULL && arg3->node_type == PT_VALUE && arg3_type == PT_TYPE_INTEGER);
9413  /* change locale from date_lang (set by grammar) to number_lang */
9414  (void) lang_get_lang_id_from_flag (arg3->info.value.data_value.i, &has_user_format, &has_user_lang);
9415  if (!has_user_lang)
9416  {
9417  int lang_flag;
9419  (void) lang_set_flag_from_lang (lang_str, has_user_format, has_user_lang, &lang_flag);
9420  arg3->info.value.data_value.i = lang_flag;
9421  arg3->info.value.db_value_is_initialized = 0;
9422  pt_value_to_db (parser, arg3);
9423  }
9424  }
9425 
9426  break;
9427 
9428  case PT_FROM_TZ:
9429  case PT_NEW_TIME:
9430  {
9431  if (arg1_type != PT_TYPE_DATETIME && arg1_type != PT_TYPE_TIME && arg1_type != PT_TYPE_MAYBE)
9432  {
9433  node->type_enum = PT_TYPE_NULL;
9434  goto error;
9435  }
9436  }
9437  break;
9438 
9439  default:
9440  break;
9441  }
9442 
9443  if (pt_apply_expressions_definition (parser, &expr) != NO_ERROR)
9444  {
9445  expr = NULL;
9446  node->type_enum = PT_TYPE_NONE;
9447  goto error;
9448  }
9449 
9450  if (expr != NULL && PT_GET_COLLATION_MODIFIER (expr) != -1)
9451  {
9452  if (!PT_HAS_COLLATION (arg1_type) && !PT_HAS_COLLATION (arg2_type) && !PT_HAS_COLLATION (arg3_type)
9453  && !PT_HAS_COLLATION (node->type_enum) && (expr->info.expr.op != PT_CAST || arg1_type != PT_TYPE_MAYBE))
9454  {
9455  if (!pt_has_error (parser))
9456  {
9458  }
9459  node->type_enum = PT_TYPE_NONE;
9460  goto error;
9461  }
9462  }
9463 
9464  if (expr != NULL)
9465  {
9466  assert (check_expr_coll);
9467  if (pt_check_expr_collation (parser, &expr) != NO_ERROR)
9468  {
9469  expr = NULL;
9470  node->type_enum = PT_TYPE_NONE;
9471  return node;
9472  }
9473  }
9474 
9475  check_expr_coll = false;
9476 
9477  if (expr != NULL)
9478  {
9479  expr = pt_wrap_expr_w_exp_dom_cast (parser, expr);
9480 
9481  node = expr;
9482  expr = NULL;
9483 
9484  switch (op)
9485  {
9486  case PT_BETWEEN:
9487  case PT_NOT_BETWEEN:
9488  /* between and rage operators are written like: PT_BETWEEN(arg1, PT_BETWEEN_AND(arg2,arg3)) We convert it to
9489  * PT_BETWEEN(arg1, arg2, arg2) to be able to decide the correct common type of all arguments and we will
9490  * convert it back once we apply the correct casts */
9491  if (arg2->node_type == PT_EXPR && pt_is_between_range_op (arg2->info.expr.op))
9492  {
9493  arg2->info.expr.arg1 = node->info.expr.arg2;
9494  arg2->info.expr.arg2 = node->info.expr.arg3;
9495  node->info.expr.arg2 = arg2;
9496  node->info.expr.arg3 = NULL;
9497  }
9498  break;
9499 
9500  case PT_LIKE:
9501  case PT_NOT_LIKE:
9502  /* convert PT_LIKE(arg1, arg2, arg3) back to PT_LIKE(arg1, PT_LIKE_ESCAPE(arg2, arg3)) A better approach
9503  * would be to modify the parser to output PT_LIKE(arg1, arg2, arg3) directly. */
9504  if (arg2->node_type == PT_EXPR && arg2->info.expr.op == PT_LIKE_ESCAPE)
9505  {
9506 
9507  arg2->info.expr.arg1 = node->info.expr.arg2;
9508  arg2->info.expr.arg2 = node->info.expr.arg3;
9509  node->info.expr.arg2 = arg2;
9510  node->info.expr.arg3 = NULL;
9511  }
9512  break;
9513 
9514  case PT_RAND:
9515  case PT_RANDOM:
9516  case PT_DRAND:
9517  case PT_DRANDOM:
9518  /* to keep mysql compatibility we should consider a NULL argument as the value 0. This is the only place
9519  * where we can perform this check */
9520  arg1 = node->info.expr.arg1;
9521  if (arg1 && arg1->type_enum == PT_TYPE_NULL && arg1->node_type == PT_VALUE)
9522  {
9523  arg1->type_enum = arg1_type = PT_TYPE_INTEGER;
9524  db_make_int (&arg1->info.value.db_value, 0);
9525  }
9526  break;
9527 
9528  case PT_EXTRACT:
9529  if (arg1_type == PT_TYPE_MAYBE)
9530  {
9531  assert (node->type_enum == PT_TYPE_INTEGER);
9532  }
9533  else if (arg1_type == PT_TYPE_NA || arg1_type == PT_TYPE_NULL)
9534  {
9535  node->type_enum = arg1_type;
9536  }
9537  else if (PT_IS_CHAR_STRING_TYPE (arg1_type) || PT_IS_DATE_TIME_TYPE (arg1_type))
9538  {
9539  int incompatible_extract_type = false;
9540 
9541  node->type_enum = PT_TYPE_NONE;
9542  switch (node->info.expr.qualifier)
9543  {
9544  case PT_YEAR:
9545  case PT_MONTH:
9546  case PT_DAY:
9547  if (PT_IS_CHAR_STRING_TYPE (arg1_type))
9548  {
9549  arg1_type = PT_TYPE_NONE;
9550  if (pt_check_and_coerce_to_date (parser, arg1) == NO_ERROR)
9551  {
9552  arg1_type = PT_TYPE_DATE;
9553  }
9554  else
9555  {
9556  parser_free_tree (parser, parser->error_msgs);
9557  parser->error_msgs = NULL;
9558 
9559  /* try coercing to utime */
9560  if (pt_coerce_value (parser, arg1, arg1, PT_TYPE_TIMESTAMP, NULL) == NO_ERROR)
9561  {
9562  arg1_type = PT_TYPE_TIMESTAMP;
9563  }
9564  else
9565  {
9566  parser_free_tree (parser, parser->error_msgs);
9567  parser->error_msgs = NULL;
9568 
9569  /* try coercing to datetime */
9570  if (pt_coerce_value (parser, arg1, arg1, PT_TYPE_DATETIME, NULL) == NO_ERROR)
9571  {
9572  arg1_type = PT_TYPE_DATETIME;
9573  }
9574  }
9575  }
9576  }
9577 
9578  if (PT_HAS_DATE_PART (arg1_type))
9579  {
9580  node->type_enum = PT_TYPE_INTEGER;
9581  }
9582  else if (arg1_type == PT_TYPE_TIME)
9583  {
9584  incompatible_extract_type = true;
9585  }
9586  break;
9587 
9588  case PT_HOUR:
9589  case PT_MINUTE:
9590  case PT_SECOND:
9591  if (PT_IS_CHAR_STRING_TYPE (arg1_type))
9592  {
9593  arg1_type = PT_TYPE_NONE;
9594  if (pt_check_and_coerce_to_time (parser, arg1) == NO_ERROR)
9595  {
9596  arg1_type = PT_TYPE_TIME;
9597  }
9598  else
9599  {
9600  parser_free_tree (parser, parser->error_msgs);
9601  parser->error_msgs = NULL;
9602 
9603  /* try coercing to utime */
9604  if (pt_coerce_value (parser, arg1, arg1, PT_TYPE_TIMESTAMP, NULL) == NO_ERROR)
9605  {
9606  arg1_type = PT_TYPE_TIMESTAMP;
9607  }
9608  else
9609  {
9610  parser_free_tree (parser, parser->error_msgs);
9611  parser->error_msgs = NULL;
9612 
9613  /* try coercing to datetime */
9614  if (pt_coerce_value (parser, arg1, arg1, PT_TYPE_DATETIME, NULL) == NO_ERROR)
9615  {
9616  arg1_type = PT_TYPE_DATETIME;
9617  }
9618  }
9619  }
9620  }
9621 
9622  if (PT_HAS_TIME_PART (arg1_type))
9623  {
9624  node->type_enum = PT_TYPE_INTEGER;
9625  }
9626  else if (arg1_type == PT_TYPE_DATE)
9627  {
9628  incompatible_extract_type = true;
9629  }
9630  break;
9631 
9632  case PT_MILLISECOND:
9633  if (PT_IS_CHAR_STRING_TYPE (arg1_type))
9634  {
9635  arg1_type = PT_TYPE_NONE;
9636  /* try coercing to datetime */
9637  if (pt_coerce_value (parser, arg1, arg1, PT_TYPE_DATETIME, NULL) == NO_ERROR)
9638  {
9639  arg1_type = PT_TYPE_DATETIME;
9640  }
9641  }
9642 
9643  if (arg1_type == PT_TYPE_DATETIME || arg1_type == PT_TYPE_DATETIMELTZ
9644  || arg1_type == PT_TYPE_DATETIMETZ)
9645  {
9646  node->type_enum = PT_TYPE_INTEGER;
9647  }
9648  else if (arg1_type == PT_TYPE_DATE || arg1_type == PT_TYPE_TIME || arg1_type == PT_TYPE_TIMESTAMP
9649  || arg1_type == PT_TYPE_TIMESTAMPLTZ || arg1_type == PT_TYPE_TIMESTAMPTZ)
9650  {
9651  incompatible_extract_type = true;
9652  }
9653  break;
9654  default:
9655  break;
9656  }
9657 
9658  if (incompatible_extract_type)
9659  {
9661  pt_show_misc_type (node->info.expr.qualifier), pt_show_type_enum (arg1->type_enum));
9662  return node;
9663  }
9664 
9665  if (node->type_enum != PT_TYPE_NONE && (node->data_type = parser_new_node (parser, PT_DATA_TYPE)) != NULL)
9666  {
9667  node->data_type->type_enum = node->type_enum;
9668  }
9669  }
9670  else
9671  {
9672  /* argument is not date, string, MAYBE or NULL */
9673  node->type_enum = PT_TYPE_NONE;
9674  }
9675  break;
9676 
9677  case PT_COALESCE:
9678  if (common_type != PT_TYPE_NONE && arg1_type != PT_TYPE_NA && arg1_type != PT_TYPE_NULL
9679  && arg2_type != PT_TYPE_NA && arg2_type != PT_TYPE_NULL && PT_IS_COLLECTION_TYPE (common_type))
9680  {
9681  pt_propagate_types (parser, node, arg1->data_type, arg2->data_type);
9682  }
9683  break;
9684 
9685  case PT_TIMEDIFF:
9686  if (PT_IS_DATE_TIME_TYPE (arg1_type) && PT_IS_DATE_TIME_TYPE (arg2_type))
9687  {
9688  if (arg1_type == PT_TYPE_TIME || arg1_type == PT_TYPE_DATE)
9689  {
9690  if (arg2_type != arg1_type)
9691  {
9692  node->type_enum = PT_TYPE_NONE;
9693  }
9694  }
9695  else
9696  {
9697  /* arg1_type is PT_TYPE_DATETIME or PT_TYPE_TIMESTAMP. */
9698  if (arg2_type == PT_TYPE_TIME || arg2_type == PT_TYPE_DATE)
9699  {
9700  node->type_enum = PT_TYPE_NONE;
9701  }
9702  }
9703  }
9704  break;
9705 
9706  case PT_FROM_TZ:
9707  if (arg1_type == PT_TYPE_DATETIME)
9708  {
9709  node->type_enum = PT_TYPE_DATETIMETZ;
9710  }
9711  break;
9712 
9713  default:
9714  break;
9715  }
9716  goto error;
9717  }
9718 
9719  if (pt_is_symmetric_op (op))
9720  {
9721  /*
9722  * At most one of these next two cases will hold... these will
9723  * make a dynamic host var (one about whose type we know nothing
9724  * at this point) assume the type of its "mate" in a symmetric
9725  * dyadic operator.
9726  */
9727  if (arg1_hv && arg2_type != PT_TYPE_NONE && arg2_type != PT_TYPE_MAYBE)
9728  {
9729  if (arg1_hv != arg1)
9730  {
9731  /* special case of the unary minus on host var no error will be returned in this case */
9732  (void) pt_coerce_value (parser, arg1_hv, arg1_hv, arg2_type, arg2->data_type);
9733  arg1_type = arg1->type_enum = arg1_hv->type_enum;
9734  d = pt_xasl_type_enum_to_domain (arg1_type);
9735  SET_EXPECTED_DOMAIN (arg1, d);
9736  SET_EXPECTED_DOMAIN (arg1_hv, d);
9737  pt_preset_hostvar (parser, arg1_hv);
9738  }
9739  else
9740  {
9741  /* no error will be returned in this case */
9742  (void) pt_coerce_value (parser, arg1, arg1, arg2_type, arg2->data_type);
9743  arg1_type = arg1->type_enum;
9744  d = pt_xasl_type_enum_to_domain (arg1_type);
9745  SET_EXPECTED_DOMAIN (arg1, d);
9746  pt_preset_hostvar (parser, arg1);
9747  }
9748  }
9749 
9750  if (arg2_hv && arg1_type != PT_TYPE_NONE && arg1_type != PT_TYPE_MAYBE)
9751  {
9752  if (arg2_hv != arg2)
9753  {
9754  /* special case of the unary minus on host var no error will be returned in this case */
9755  (void) pt_coerce_value (parser, arg2_hv, arg2_hv, arg1_type, arg1->data_type);
9756  arg2_type = arg2->type_enum = arg2_hv->type_enum;
9757  d = pt_xasl_type_enum_to_domain (arg2_type);
9758  SET_EXPECTED_DOMAIN (arg2, d);
9759  SET_EXPECTED_DOMAIN (arg2_hv, d);
9760  pt_preset_hostvar (parser, arg2_hv);
9761  }
9762  else
9763  {
9764  /* no error will be returned in this case */
9765  (void) pt_coerce_value (parser, arg2, arg2, arg1_type, arg1->data_type);
9766  arg2_type = arg2->type_enum;
9767  d = pt_xasl_type_enum_to_domain (arg2_type);
9768  SET_EXPECTED_DOMAIN (arg2, d);
9769  pt_preset_hostvar (parser, arg2);
9770  }
9771  }
9772 
9773  if (arg2)
9774  {
9775  if (pt_is_enumeration_special_comparison (arg1, op, arg2))
9776  {
9777  /* In case of 'ENUM = const' or 'ENUM IN ...' we need to convert the right argument to the ENUM type in
9778  * order to preserve an eventual index scan on left argument */
9779  common_type = PT_TYPE_ENUMERATION;
9780  }
9781  else
9782  {
9783  common_type = pt_common_type_op (arg1_type, op, arg2_type);
9784  }
9785  }
9786 
9787  if (pt_is_symmetric_type (common_type))
9788  {
9789  PT_NODE *data_type;
9790 
9791  if (arg1_type != common_type)
9792  {
9793  /*
9794  * pt_coerce_value may fail here, but it shouldn't be
9795  * considered a real failure yet, because it could still
9796  * be rescued by the gruesome date/time stuff below.
9797  * DON'T set common_type here, or you'll surely screw up
9798  * the next case when you least expect it.
9799  */
9800  if (PT_IS_NUMERIC_TYPE (common_type) || PT_IS_STRING_TYPE (common_type))
9801  {
9802  data_type = NULL;
9803  }
9804  else if (PT_IS_COLLECTION_TYPE (common_type))
9805  {
9806  data_type = arg1->data_type;
9807  }
9808  else
9809  {
9810  data_type = arg2->data_type;
9811  }
9812 
9813  pt_coerce_value (parser, arg1, arg1, common_type, data_type);
9814  arg1_type = arg1->type_enum;
9815  }
9816 
9817  if (arg2 && arg2_type != common_type)
9818  {
9819  /* Same warning as above... */
9820  if (PT_IS_NUMERIC_TYPE (common_type) || PT_IS_STRING_TYPE (common_type))
9821  {
9822  data_type = NULL;
9823  }
9824  else if (PT_IS_COLLECTION_TYPE (common_type))
9825  {
9826  data_type = arg2->data_type;
9827  }
9828  else
9829  {
9830  data_type = arg1->data_type;
9831  }
9832 
9833  pt_coerce_value (parser, arg2, arg2, common_type, data_type);
9834  arg2_type = arg2->type_enum;
9835  }
9836  }
9837  }
9838  else
9839  {
9841  {
9842  if (arg2 && arg2->type_enum == PT_TYPE_MAYBE)
9843  {
9844  if (PT_IS_NUMERIC_TYPE (arg1_type) || PT_IS_STRING_TYPE (arg1_type))
9845  {
9846  d = pt_node_to_db_domain (parser, arg1, NULL);
9847  d = tp_domain_cache (d);
9848  SET_EXPECTED_DOMAIN (arg2, d);
9849  if (arg2->node_type == PT_HOST_VAR)
9850  {
9851  pt_preset_hostvar (parser, arg2);
9852  }
9853  }
9854  }
9855 
9856  if (PT_IS_FUNCTION (arg2) && PT_IS_COLLECTION_TYPE (arg2->type_enum))
9857  {
9858  /* a IN (?, ...) */
9859  PT_NODE *temp;
9860 
9861  for (temp = arg2->info.function.arg_list; temp; temp = temp->next)
9862  {
9863  if (temp->node_type == PT_HOST_VAR && temp->type_enum == PT_TYPE_MAYBE)
9864  {
9865  if (arg1_type != PT_TYPE_NONE && arg1_type != PT_TYPE_MAYBE)
9866  {
9867  (void) pt_coerce_value (parser, temp, temp, arg1_type, arg1->data_type);
9869  SET_EXPECTED_DOMAIN (temp, d);
9870  if (temp->node_type == PT_HOST_VAR)
9871  {
9872  pt_preset_hostvar (parser, temp);
9873  }
9874  }
9875  }
9876  }
9877  }
9878 
9879  if (arg3 && arg3->type_enum == PT_TYPE_MAYBE)
9880  {
9881  if (PT_IS_NUMERIC_TYPE (arg1_type) || PT_IS_STRING_TYPE (arg1_type))
9882  {
9883  d = pt_node_to_db_domain (parser, arg1, NULL);
9884  d = tp_domain_cache (d);
9885  SET_EXPECTED_DOMAIN (arg3, d);
9886  if (arg3->node_type == PT_HOST_VAR)
9887  {
9888  pt_preset_hostvar (parser, arg3);
9889  }
9890  }
9891  }
9892  }
9893  }
9894 
9895  if ((common_type == PT_TYPE_NA || common_type == PT_TYPE_NULL) && node->or_next)
9896  {
9897  common_type = node->or_next->type_enum;
9898  }
9899 
9900  node->type_enum = common_type;
9901 
9902  if (node->type_enum == PT_TYPE_MAYBE && pt_is_able_to_determine_return_type (op))
9903  {
9904  /* Because we can determine the return type of the expression regardless of its argument, go further to determine
9905  * it. temporary reset to NONE. */
9906  node->type_enum = PT_TYPE_NONE;
9907  }
9908 
9909  if (node->type_enum == PT_TYPE_MAYBE)
9910  {
9911  /* There can be a unbinded host variable at compile time */
9913  {
9914  /* don't touch the args. leave it as it is */
9915  return node;
9916  }
9917 
9918  if (arg1_type == PT_TYPE_MAYBE)
9919  {
9920  if (node->expected_domain
9921  && (TP_IS_NUMERIC_TYPE (TP_DOMAIN_TYPE (node->expected_domain))
9922  || TP_IS_STRING_TYPE (TP_DOMAIN_TYPE (node->expected_domain))))
9923  {
9924  SET_EXPECTED_DOMAIN (arg1, node->expected_domain);
9925  if (arg1->node_type == PT_HOST_VAR)
9926  {
9927  pt_preset_hostvar (parser, arg1);
9928  }
9929  }
9930  else if (arg2 && (PT_IS_NUMERIC_TYPE (arg2_type) || PT_IS_STRING_TYPE (arg2_type)))
9931  {
9932  if (arg1->node_type == PT_NAME)
9933  { /* subquery derived table */
9934  PT_NODE *new_att;
9935  int p, s;
9936 
9937  if (arg2->data_type)
9938  {
9939  p = arg2->data_type->info.data_type.precision;
9940  s = arg2->data_type->info.data_type.dec_precision;
9941  }
9942  else
9943  {
9946  }
9947 
9948  new_att = pt_wrap_with_cast_op (parser, arg1, arg2_type, p, s, arg2->data_type);
9949  if (new_att == NULL)
9950  {
9951  node->type_enum = PT_TYPE_NONE;
9952  goto error;
9953  }
9954  node->info.expr.arg1 = arg1 = new_att;
9955  arg1_type = arg2_type;
9956  }
9957  else
9958  { /* has a hostvar */
9959  d = pt_node_to_db_domain (parser, arg2, NULL);
9960  d = tp_domain_cache (d);
9961  SET_EXPECTED_DOMAIN (arg1, d);
9962  if (arg1->node_type == PT_HOST_VAR)
9963  {
9964  pt_preset_hostvar (parser, arg1);
9965  }
9966  }
9967  }
9968  }
9969 
9970  if (arg2_type == PT_TYPE_MAYBE)
9971  {
9972  if (node->expected_domain
9973  && (TP_IS_NUMERIC_TYPE (TP_DOMAIN_TYPE (node->expected_domain))
9974  || TP_IS_STRING_TYPE (TP_DOMAIN_TYPE (node->expected_domain))))
9975  {
9976  SET_EXPECTED_DOMAIN (arg2, node->expected_domain);
9977  if (arg2->node_type == PT_HOST_VAR)
9978  {
9979  pt_preset_hostvar (parser, arg2);
9980  }
9981  }
9982  else if (arg1 && (PT_IS_NUMERIC_TYPE (arg1_type) || PT_IS_STRING_TYPE (arg1_type)))
9983  {
9984  if (arg2->node_type == PT_NAME)
9985  { /* subquery derived table */
9986  PT_NODE *new_att;
9987  int p, s;
9988 
9989  if (arg1->data_type)
9990  {
9991  p = arg1->data_type->info.data_type.precision;
9993  }
9994  else
9995  {
9998  }
9999 
10000  new_att = pt_wrap_with_cast_op (parser, arg2, arg1_type, p, s, arg1->data_type);
10001  if (new_att == NULL)
10002  {
10003  node->type_enum = PT_TYPE_NONE;
10004  goto error;
10005  }
10006  node->info.expr.arg2 = arg2 = new_att;
10007  arg2_type = arg1_type;
10008  }
10009  else
10010  { /* has a hostvar */
10011  d = pt_node_to_db_domain (parser, arg1, NULL);
10012  d = tp_domain_cache (d);
10013  SET_EXPECTED_DOMAIN (arg2, d);
10014  if (arg2->node_type == PT_HOST_VAR)
10015  {
10016  pt_preset_hostvar (parser, arg2);
10017  }
10018  }
10019  }
10020  }
10021 
10022  return node;
10023  } /* if node->type_enum == PT_TYPE_MAYBE */
10024 
10025  switch (op)
10026  {
10027  case PT_IF:
10028  if (arg1_type == PT_TYPE_MAYBE)
10029  {
10031  SET_EXPECTED_DOMAIN (arg1, d);
10032  pt_preset_hostvar (parser, arg1);
10033  }
10034  else if (arg1_type != PT_TYPE_LOGICAL && arg2_type != PT_TYPE_NULL)
10035  {
10036  node->type_enum = PT_TYPE_NONE;
10037  break;
10038  }
10039  if (arg2_type == PT_TYPE_MAYBE || arg3_type == PT_TYPE_MAYBE)
10040  {
10041  if (arg2_type == PT_TYPE_MAYBE && arg3_type == PT_TYPE_MAYBE)
10042  {
10043  if (node->expected_domain != NULL)
10044  {
10045  /* the expected domain might have been set by a different pass through the tree */
10046  common_type = pt_db_to_type_enum (TP_DOMAIN_TYPE (node->expected_domain));
10047  }
10048  else
10049  {
10050  common_type = PT_TYPE_VARCHAR;
10051  }
10052  }
10053  else
10054  {
10055  common_type = (arg2_type == PT_TYPE_MAYBE) ? arg3_type : arg2_type;
10056  }
10057  }
10058  else
10059  {
10060  /* We have to decide a common type for arg2 and arg3. We cannot use pt_common_type_op because this function
10061  * is designed mostly for arithmetic expression. This is why we use the tp_is_more_general_type here. */
10062  dbtype2 = pt_type_enum_to_db (arg2_type);
10063  dbtype3 = pt_type_enum_to_db (arg3_type);
10064  if (tp_more_general_type (dbtype2, dbtype3) > 0)
10065  {
10066  common_type = arg2_type;
10067  }
10068  else
10069  {
10070  common_type = arg3_type;
10071  }
10072  }
10073  if (common_type == PT_TYPE_LOGICAL)
10074  {
10075  /* we will end up with logical here if arg3 and arg2 are logical */
10076  common_type = PT_TYPE_INTEGER;
10077  }
10078  if (pt_coerce_expression_argument (parser, node, &arg2, common_type, NULL) != NO_ERROR)
10079  {
10080  node->type_enum = PT_TYPE_NONE;
10081  break;
10082  }
10083  node->info.expr.arg2 = arg2;
10084 
10085  if (pt_coerce_expression_argument (parser, node, &arg3, common_type, NULL) != NO_ERROR)
10086  {
10087  node->type_enum = PT_TYPE_NONE;
10088  break;
10089  }
10090  node->info.expr.arg3 = arg3;
10091  node->type_enum = common_type;
10092 
10093  if (PT_HAS_COLLATION (common_type))
10094  {
10095  check_expr_coll = true;
10096  }
10097  break;
10098 
10099  case PT_FIELD:
10100  if ((arg1 && arg1_type == PT_TYPE_LOGICAL) || (arg2 && arg2_type == PT_TYPE_LOGICAL)
10101  || pt_list_has_logical_nodes (arg3))
10102  {
10103  node->type_enum = PT_TYPE_NONE;
10104  break;
10105  }
10106 
10107  check_expr_coll = true;
10108 
10109  node->type_enum = common_type = PT_TYPE_INTEGER;
10110 
10111  if (arg3_type != PT_TYPE_NULL && arg3_type != PT_TYPE_NA && arg3_type != PT_TYPE_MAYBE)
10112  {
10113  first_node = (arg3->next && arg3->next->info.value.data_value.i == 1);
10114 
10115  if (!((first_node ? PT_IS_STRING_TYPE (arg1_type) : true)
10116  && (arg2_type != PT_TYPE_NULL && arg2_type != PT_TYPE_NA ? PT_IS_STRING_TYPE (arg2_type) : true)
10117  && (PT_IS_STRING_TYPE (arg3_type) || arg3_type == PT_TYPE_ENUMERATION))
10118  && !((first_node ? PT_IS_NUMERIC_TYPE (arg1_type) : true)
10119  && (arg2_type != PT_TYPE_NULL && arg2_type != PT_TYPE_NA ? PT_IS_NUMERIC_TYPE (arg2_type) : true)
10120  && (PT_IS_NUMERIC_TYPE (arg3_type) || arg3_type == PT_TYPE_ENUMERATION)))
10121  {
10122  /* cast to type of first parameter */
10123 
10124  if (arg3_type == PT_TYPE_CHAR)
10125  {
10126  new_type = PT_TYPE_VARCHAR;
10127  }
10128  else if (arg3_type == PT_TYPE_NCHAR)
10129  {
10130  new_type = PT_TYPE_VARNCHAR;
10131  }
10132  else
10133  {
10134  new_type = arg3_type;
10135  }
10136 
10137  if (first_node)
10138  {
10139  new_att = pt_wrap_with_cast_op (parser, arg1, new_type, TP_FLOATING_PRECISION_VALUE, 0, NULL);
10140  if (new_att == NULL)
10141  {
10142  node->type_enum = PT_TYPE_NONE;
10143  goto error;
10144  }
10145  assert (new_att->node_type == PT_EXPR);
10147  node->info.expr.arg1 = arg1 = new_att;
10148  arg1_type = new_type;
10149  }
10150 
10151  new_att = pt_wrap_with_cast_op (parser, arg2, new_type, TP_FLOATING_PRECISION_VALUE, 0, NULL);
10152  if (new_att == NULL)
10153  {
10154  node->type_enum = PT_TYPE_NONE;
10155  goto error;
10156  }
10157  assert (new_att->node_type == PT_EXPR);
10159  node->info.expr.arg2 = arg2 = new_att;
10160  arg2_type = new_type;
10161  }
10162  }
10163  break;
10164 
10165  case PT_CONNECT_BY_ROOT:
10166  node->type_enum = node->info.expr.arg1->type_enum;
10167  break;
10168 
10169  case PT_ASSIGN:
10170  node->data_type = parser_copy_tree_list (parser, arg1->data_type);
10171  node->type_enum = arg1_type;
10172 
10173  if (PT_IS_HOSTVAR (arg2) && arg2->expected_domain == NULL)
10174  {
10175  d = pt_node_to_db_domain (parser, arg1, NULL);
10176  d = tp_domain_cache (d);
10177  SET_EXPECTED_DOMAIN (arg2, d);
10178  pt_preset_hostvar (parser, arg2);
10179  }
10180  break;
10181 
10182  case PT_LIKE_ESCAPE:
10183  /* PT_LIKE_ESCAPE is handled by PT_LIKE */
10184  break;
10185 
10186  case PT_EXISTS:
10187  if (common_type != PT_TYPE_NONE && (pt_is_query (arg1) || PT_IS_COLLECTION_TYPE (arg1_type)))
10188  {
10189  node->type_enum = PT_TYPE_LOGICAL;
10190  if (pt_is_query (arg1))
10191  {
10192  pt_chop_to_one_select_item (parser, arg1);
10193  }
10194  }
10195  else if (arg1_type == PT_TYPE_NA || arg1_type == PT_TYPE_NULL)
10196  {
10197  node->type_enum = PT_TYPE_NULL;
10198  }
10199  else
10200  {
10201  node->type_enum = PT_TYPE_NONE;
10202  }
10203  break;
10204 
10205  case PT_RANGE:
10206  node->type_enum = PT_TYPE_NONE;
10207  if (arg2 == NULL)
10208  {
10209  goto error;
10210  }
10211  node->type_enum = PT_TYPE_LOGICAL;
10212  break;
10213 
10215  /* The argument should already have the type of the spec's OID; see pt_dup_key_update_stmt () */
10216  node->data_type = parser_copy_tree (parser, node->info.expr.arg1->data_type);
10217  node->type_enum = PT_TYPE_OBJECT;
10218  break;
10219 
10220  case PT_STR_TO_DATE:
10221  {
10222  int type_specifier = 0;
10223 
10224  assert (arg3_type == PT_TYPE_INTEGER);
10225 
10226  if (arg2_type == PT_TYPE_NULL)
10227  {
10228  node->type_enum = PT_TYPE_NULL;
10229  break;
10230  }
10231  if (!PT_IS_VALUE_NODE (arg2) && !PT_IS_INPUT_HOSTVAR (arg2)
10232  && !(arg2->node_type == PT_EXPR && arg2->info.expr.op == PT_EVALUATE_VARIABLE))
10233  {
10234  node->type_enum = PT_TYPE_NONE;
10235  break;
10236  }
10237  if (!PT_IS_CHAR_STRING_TYPE (arg1_type))
10238  {
10239  if (arg1_type == PT_TYPE_MAYBE)
10240  {
10242  SET_EXPECTED_DOMAIN (arg1, d);
10243  pt_preset_hostvar (parser, arg1);
10244  }
10245  else
10246  {
10248  if (new_att == NULL)
10249  {
10250  node->type_enum = PT_TYPE_NONE;
10251  goto error;
10252  }
10254  {
10255  assert (new_att->node_type == PT_EXPR);
10257  }
10258  node->info.expr.arg1 = arg1 = new_att;
10259  }
10260  }
10261 
10262  if (arg2_hv && arg2_type == PT_TYPE_MAYBE)
10263  {
10265  SET_EXPECTED_DOMAIN (arg2, d);
10266  pt_preset_hostvar (parser, arg2);
10267  }
10268 
10269  if (arg2->node_type == PT_VALUE)
10270  {
10271  type_specifier = db_check_time_date_format ((char *) arg2->info.value.data_value.str->bytes);
10272  }
10273  else
10274  {
10275  type_specifier = PT_TYPE_MAYBE;
10276  }
10277 
10278  /* default is date (i.e.: -> when no format supplied) */
10279  node->type_enum = PT_TYPE_DATE;
10280  if (type_specifier == TIME_SPECIFIER)
10281  {
10282  node->type_enum = PT_TYPE_TIME;
10283  }
10284  else if (type_specifier == DATE_SPECIFIER)
10285  {
10286  node->type_enum = PT_TYPE_DATE;
10287  }
10288  else if (type_specifier == DATETIME_SPECIFIER)
10289  {
10290  node->type_enum = PT_TYPE_DATETIME;
10291  }
10292  else if (type_specifier == DATETIMETZ_SPECIFIER)
10293  {
10294  node->type_enum = PT_TYPE_DATETIMETZ;
10295  }
10296  else if (arg1_type == PT_TYPE_NULL || arg2_type == PT_TYPE_NULL)
10297  {
10298  /* if other value, the db_str_to_date will return NULL */
10299  node->type_enum = PT_TYPE_NULL;
10300  }
10301  else if (arg1_type == PT_TYPE_MAYBE || arg2_type == PT_TYPE_MAYBE)
10302  {
10303  node->type_enum = PT_TYPE_MAYBE;
10304  }
10305  break;
10306  }
10307 
10308  case PT_DATE_ADD:
10309  case PT_DATE_SUB:
10310  if (arg1_hv && arg1_type == PT_TYPE_MAYBE)
10311  {
10312  /* Though arg1 can be date/timestamp/datetime/string, assume it is a string which is the most general. */
10314  SET_EXPECTED_DOMAIN (arg1, d);
10315  pt_preset_hostvar (parser, arg1);
10316  common_type = node->type_enum = PT_TYPE_VARCHAR;
10317  }
10318  if (arg2_hv && arg2_type == PT_TYPE_MAYBE)
10319  {
10321  SET_EXPECTED_DOMAIN (arg2, d);
10322  pt_preset_hostvar (parser, arg2);
10323  }
10324 
10325  /* arg1 -> date or string, arg2 -> integer or string acc to unit */
10326  if ((PT_HAS_DATE_PART (arg1_type) || PT_IS_CHAR_STRING_TYPE (arg1_type) || arg1_type == PT_TYPE_MAYBE)
10327  && (PT_IS_CHAR_STRING_TYPE (arg2_type) || PT_IS_NUMERIC_TYPE (arg2_type) || arg2_type == PT_TYPE_MAYBE))
10328  {
10329  /* if arg2 is integer, unit must be one of MILLISECOND, SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER,
10330  * YEAR. */
10331  int is_single_unit;
10332 
10333  is_single_unit = (arg3
10334  && (arg3->info.expr.qualifier == PT_MILLISECOND || arg3->info.expr.qualifier == PT_SECOND
10335  || arg3->info.expr.qualifier == PT_MINUTE || arg3->info.expr.qualifier == PT_HOUR
10336  || arg3->info.expr.qualifier == PT_DAY || arg3->info.expr.qualifier == PT_WEEK
10337  || arg3->info.expr.qualifier == PT_MONTH || arg3->info.expr.qualifier == PT_QUARTER
10338  || arg3->info.expr.qualifier == PT_YEAR));
10339 
10340  if (arg1_type == PT_TYPE_DATETIMETZ || arg1_type == PT_TYPE_TIMESTAMPTZ)
10341  {
10342  node->type_enum = PT_TYPE_DATETIMETZ;
10343  }
10344  else if (arg1_type == PT_TYPE_DATETIMELTZ || arg1_type == PT_TYPE_TIMESTAMPLTZ)
10345  {
10346  node->type_enum = PT_TYPE_DATETIMELTZ;
10347  }
10348  else if (arg1_type == PT_TYPE_DATETIME || arg1_type == PT_TYPE_TIMESTAMP)
10349  {
10350  node->type_enum = PT_TYPE_DATETIME;
10351  }
10352  else if (arg1_type == PT_TYPE_DATE)
10353  {
10354  if (arg3->info.expr.qualifier == PT_DAY || arg3->info.expr.qualifier == PT_WEEK
10355  || arg3->info.expr.qualifier == PT_MONTH || arg3->info.expr.qualifier == PT_QUARTER
10356  || arg3->info.expr.qualifier == PT_YEAR || arg3->info.expr.qualifier == PT_YEAR_MONTH)
10357  {
10358  node->type_enum = PT_TYPE_DATE;
10359  }
10360  else
10361  {
10362  node->type_enum = PT_TYPE_DATETIME;
10363  }
10364  }
10365  else
10366  {
10367  node->type_enum = PT_TYPE_VARCHAR;
10368  common_type = node->type_enum;
10369  }
10370  }
10371  else if (arg1_type == PT_TYPE_NULL || arg2_type == PT_TYPE_NULL)
10372  {
10373  node->type_enum = PT_TYPE_NULL;
10374  }
10375  else
10376  {
10377  /* if we got here, we have a type incompatibility; however, if one of the arguments is of type MAYBE, the
10378  * error will not be caught. NOTE: see label 'error' at end of function */
10379  if (arg1 && arg1->type_enum == PT_TYPE_MAYBE)
10380  {
10381  arg1 = NULL;
10382  }
10383 
10384  if (arg2 && arg2->type_enum == PT_TYPE_MAYBE)
10385  {
10386  arg2 = NULL;
10387  }
10388 
10389  if (arg3 && arg3->type_enum == PT_TYPE_MAYBE)
10390  {
10391  arg3 = NULL;
10392  }
10393 
10394  /* set type to NONE so error message is shown */
10395  node->type_enum = PT_TYPE_NONE;
10396  }
10397  break;
10398  case PT_CAST:
10399  cast_type = node->info.expr.cast_type;
10400 
10402  {
10403  if (arg1->type_enum == PT_TYPE_ENUMERATION)
10404  {
10405  LANG_COLLATION *lc;
10406 
10408 
10409  /* silently rewrite the COLLATE modifier into full CAST: CAST (ENUM as STRING) */
10412  cast_type->info.data_type.units = lc->codeset;
10413  }
10414  else
10415  {
10416  /* cast_type should be the same as arg1 type */
10417  cast_type = parser_copy_tree (parser, arg1->data_type);
10418  }
10419 
10420  /* for HV argument, attempt to resolve using the expected domain of expression's node or arg1 node */
10421  if (cast_type == NULL && node->expected_domain != NULL
10422  && TP_TYPE_HAS_COLLATION (TP_DOMAIN_TYPE (node->expected_domain)))
10423  {
10424  cast_type = pt_domain_to_data_type (parser, node->expected_domain);
10425  }
10426 
10427  if (cast_type == NULL && arg1->expected_domain != NULL
10429  {
10430  /* create data type from expected domain */
10431  cast_type = pt_domain_to_data_type (parser, arg1->expected_domain);
10432  }
10433 
10434  if (cast_type != NULL)
10435  {
10436  node->info.expr.cast_type = cast_type;
10438  }
10439  }
10440 
10441  if (cast_type && pt_check_cast_op (parser, node))
10442  {
10443  node->type_enum = cast_type->type_enum;
10444  if (pt_is_set_type (cast_type))
10445  {
10446  /* use canonical set data type */
10447  node->data_type = parser_copy_tree_list (parser, cast_type->data_type);
10448  }
10449  else if (PT_IS_COMPLEX_TYPE (cast_type->type_enum))
10450  {
10451  node->data_type = parser_copy_tree_list (parser, cast_type);
10452  }
10453 
10454  /* TODO : this requires a generic fix: maybe 'arg1_hv' should never be set to arg1->info.expr.arg1; arg1 may
10455  * not be necessarily a HV node, but arg1_hv may be a direct link to argument of arg1 (in case arg1 is an
10456  * expression with unary operator)- see code at beginning of function when arguments are checked; for now,
10457  * this fix is enough for PT_CAST ( PT_UNARY_MINUS (.. ) */
10458  if (arg1_hv && arg1_type == PT_TYPE_MAYBE && arg1->node_type == PT_HOST_VAR)
10459  {
10461  d = tp_domain_cache (d);
10462  SET_EXPECTED_DOMAIN (arg1, d);
10463  pt_preset_hostvar (parser, arg1);
10464  }
10465  }
10466  else
10467  {
10468  node->type_enum = PT_TYPE_NONE;
10469  }
10470  break;
10471 
10472  case PT_DECODE:
10473  case PT_CASE:
10474  if (arg3_hv && pt_coerce_value (parser, arg3, arg3, PT_TYPE_LOGICAL, NULL) != NO_ERROR)
10475  {
10476  node->type_enum = PT_TYPE_NONE;
10477  break;
10478  }
10479 
10480  if (arg3_type != PT_TYPE_NA && arg3_type != PT_TYPE_NULL && arg3_type != PT_TYPE_LOGICAL
10481  && arg3_type != PT_TYPE_MAYBE)
10482  {
10484  return node;
10485  }
10486 
10487  arg3 = NULL;
10488  common_type = node->info.expr.recursive_type;
10489  if (common_type == PT_TYPE_NONE)
10490  {
10491  common_type = pt_common_type_op (arg1_type, op, arg2_type);
10492  }
10493  if (common_type == PT_TYPE_NONE)
10494  {
10495  node->type_enum = PT_TYPE_NONE;
10496  break;
10497  }
10498  if (common_type == PT_TYPE_MAYBE)
10499  {
10500  common_type = (arg1_type == PT_TYPE_MAYBE && arg2_type != PT_TYPE_NULL) ? arg2_type : arg1_type;
10501  }
10502 
10503  if (common_type == PT_TYPE_MAYBE)
10504  {
10505  /* both args are MAYBE, default to VARCHAR */
10506  common_type = PT_TYPE_VARCHAR;
10507  }
10508 
10509  if (PT_IS_COLLECTION_TYPE (common_type))
10510  {
10511  /* we're not casting collections */
10512  pt_propagate_types (parser, node, arg1->data_type, arg2->data_type);
10513  node->type_enum = common_type;
10514  break;
10515  }
10516  node->type_enum = common_type;
10517  if (arg1_type != common_type && arg1_type != PT_TYPE_NULL)
10518  {
10519  /* cast arg1_type to common type */
10520  if (pt_coerce_expression_argument (parser, node, &arg1, common_type, NULL) != NO_ERROR)
10521  {
10522  node->type_enum = PT_TYPE_NONE;
10523  goto error;
10524  }
10525  node->info.expr.arg1 = arg1;
10526  }
10527 
10528  if (arg2_type != common_type)
10529  {
10530  /* arg2 is a case list, we need to walk it and cast all arguments */
10531  PT_NODE *nextcase = arg2;
10532  PT_NODE *prev = node;
10533  while (nextcase)
10534  {
10535  if (nextcase->type_enum == common_type || nextcase->type_enum == PT_TYPE_NULL)
10536  {
10537  break;
10538  }
10539 
10540  if (nextcase->node_type == PT_EXPR
10541  && (nextcase->info.expr.op == PT_CASE || nextcase->info.expr.op == PT_DECODE))
10542  {
10543  /* cast nextcase->arg1 to common type */
10544  arg1 = nextcase->info.expr.arg1;
10545  arg1_type = arg1->type_enum;
10546  if (arg1_type != common_type && arg1_type != PT_TYPE_NULL)
10547  {
10548  if (pt_coerce_expression_argument (parser, nextcase, &arg1, common_type, NULL) != NO_ERROR)
10549  {
10550  /* abandon implicit casting and return error */
10551  node->type_enum = PT_TYPE_NONE;
10552  goto error;
10553  }
10554  nextcase->info.expr.arg1 = arg1;
10555  /* nextcase was already evaluated and may have a data_type set. We need to replace it with the
10556  * cast data_type */
10557  nextcase->type_enum = common_type;
10558  if (nextcase->data_type)
10559  {
10560  parser_free_tree (parser, nextcase->data_type);
10561  nextcase->data_type = parser_copy_tree_list (parser, arg1->data_type);
10562  }
10563  }
10564  /* set nextcase to nextcase->arg2 and continue */
10565  prev = nextcase;
10566  nextcase = nextcase->info.expr.arg2;
10567  continue;
10568  }
10569  else
10570  {
10571  /* cast nextcase to common type */
10572  if (pt_coerce_expression_argument (parser, prev, &nextcase, common_type, NULL) != NO_ERROR)
10573  {
10574  /* abandon implicit casting and return error */
10575  node->type_enum = PT_TYPE_NONE;
10576  goto error;
10577  }
10578  prev->info.expr.arg2 = nextcase;
10579  nextcase = NULL;
10580  }
10581  }
10582  }
10583  break;
10584 
10585  case PT_PATH_EXPR_SET:
10586  /* temporary setting with the first element */
10587  node->type_enum = node->info.expr.arg1->type_enum;
10588  break;
10589 
10590  default:
10591  node->type_enum = PT_TYPE_NONE;
10592  break;
10593  }
10594 
10595  if (PT_IS_PARAMETERIZED_TYPE (common_type)
10596  && pt_upd_domain_info (parser, (op == PT_IF) ? arg3 : arg1, arg2, op, common_type, node) != NO_ERROR)
10597  {
10598  node->type_enum = PT_TYPE_NONE;
10599  }
10600 
10601 cannot_use_signature:
10602  if (expr != NULL)
10603  {
10604  expr = pt_wrap_expr_w_exp_dom_cast (parser, expr);
10605  node = expr;
10606  expr = NULL;
10607  }
10608 
10609 error:
10610  if (node->type_enum == PT_TYPE_NONE)
10611  {
10612  if ((arg1 && arg1->type_enum == PT_TYPE_MAYBE) || (arg2 && arg2->type_enum == PT_TYPE_MAYBE)
10613  || (arg3 && arg3->type_enum == PT_TYPE_MAYBE))
10614  {
10615  node->type_enum = PT_TYPE_MAYBE;
10616  }
10617  }
10618 
10619  if (check_expr_coll && node->type_enum != PT_TYPE_NONE)
10620  {
10621  if (pt_check_expr_collation (parser, &node) != NO_ERROR)
10622  {
10623  node->type_enum = PT_TYPE_NONE;
10624  return node;
10625  }
10626  }
10627 
10628  if (node->type_enum == PT_TYPE_NONE)
10629  {
10630  if (!pt_has_error (parser))
10631  {
10632  if (arg2 && arg3)
10633  {
10634  assert (arg1 != NULL);
10636  pt_show_binopcode (op), pt_show_type_enum (arg1->type_enum),
10637  pt_show_type_enum (arg2->type_enum), pt_show_type_enum (arg3->type_enum));
10638  }
10639  else if (arg2)
10640  {
10641  assert (arg1 != NULL);
10643  pt_show_binopcode (op), pt_show_type_enum (arg1->type_enum),
10644  pt_show_type_enum (arg2->type_enum));
10645  }
10646  else if (arg1)
10647  {
10649  pt_show_binopcode (op), pt_show_type_enum (arg1->type_enum));
10650  }
10651  else
10652  {
10655  }
10656  }
10657  }
10658 
10659  return node;
10660 }
10661 
10662 /*
10663  * pt_eval_opt_type () -
10664  * return:
10665  * parser(in):
10666  * node(in/out):
10667  */
10668 static PT_NODE *
10669 pt_eval_opt_type (PARSER_CONTEXT * parser, PT_NODE * node)
10670 {
10672  PT_NODE *arg1, *arg2;
10673 
10674  switch (node->node_type)
10675  {
10676  case PT_GET_OPT_LVL:
10677  option = node->info.get_opt_lvl.option;
10678  if (option == PT_OPT_COST)
10679  {
10680  arg1 = node->info.get_opt_lvl.args;
10681  if (PT_IS_CHAR_STRING_TYPE (arg1->type_enum))
10682  {
10683  node->type_enum = PT_TYPE_VARCHAR;
10684  }
10685  else
10686  {
10689  node->type_enum = PT_TYPE_NONE;
10690  node = NULL;
10691  }
10692  }
10693  else
10694  {
10695  node->type_enum = PT_TYPE_INTEGER;
10696  }
10697  break;
10698 
10699  case PT_SET_OPT_LVL:
10700  node->type_enum = PT_TYPE_NONE;
10701  option = node->info.set_opt_lvl.option;
10702  arg1 = node->info.set_opt_lvl.val;
10703 
10704  switch (option)
10705  {
10706  case PT_OPT_LVL:
10707  if (arg1->type_enum != PT_TYPE_INTEGER)
10708  {
10711  node = NULL;
10712  }
10713  break;
10714 
10715  case PT_OPT_COST:
10716  arg2 = arg1->next;
10718  {
10721  node = NULL;
10722  }
10723  break;
10724 
10725  default:
10726  break;
10727  }
10728  break;
10729 
10730  default:
10731  break;
10732  }
10733 
10734  return node;
10735 }
10736 
10737 /*
10738  * pt_common_type () -
10739  * return: returns the type into which these two types can be coerced
10740  * or PT_TYPE_NONE if no such type exists
10741  * arg1_type(in): a data type
10742  * arg2_type(in): a data type
10743  */
10744 
10747 {
10748  PT_TYPE_ENUM common_type = PT_TYPE_NONE;
10749 
10750  if (arg1_type == arg2_type)
10751  {
10752  if (arg1_type == PT_TYPE_ENUMERATION)
10753  {
10754  /* The common type between two ENUMs is string */
10755  common_type = PT_TYPE_VARCHAR;
10756  }
10757  else
10758  {
10759  common_type = arg1_type;
10760  }
10761  }
10762  else if ((PT_IS_NUMERIC_TYPE (arg1_type) && PT_IS_STRING_TYPE (arg2_type))
10763  || (PT_IS_NUMERIC_TYPE (arg2_type) && PT_IS_STRING_TYPE (arg1_type)))
10764  {
10765  common_type = PT_TYPE_DOUBLE;
10766  }
10767  else if ((PT_IS_STRING_TYPE (arg1_type) && arg2_type == PT_TYPE_JSON)
10768  || (arg1_type == PT_TYPE_JSON && PT_IS_STRING_TYPE (arg2_type)))
10769  {
10770  common_type = PT_TYPE_JSON;
10771  }
10772  else if ((PT_IS_NUMERIC_TYPE (arg1_type) && arg2_type == PT_TYPE_MAYBE)
10773  || (PT_IS_NUMERIC_TYPE (arg2_type) && arg1_type == PT_TYPE_MAYBE))
10774  {
10775  common_type = PT_TYPE_DOUBLE;
10776  }
10777  else
10778  {
10779  switch (arg1_type)
10780  {
10781  case PT_TYPE_DOUBLE:
10782  switch (arg2_type)
10783  {
10784  case PT_TYPE_SMALLINT:
10785  case PT_TYPE_INTEGER:
10786  case PT_TYPE_BIGINT:
10787  case PT_TYPE_FLOAT:
10788  case PT_TYPE_DOUBLE:
10789  case PT_TYPE_NUMERIC:
10790  case PT_TYPE_LOGICAL:
10791  case PT_TYPE_ENUMERATION:
10792  common_type = PT_TYPE_DOUBLE;
10793  break;
10794  case PT_TYPE_MONETARY:
10795  common_type = PT_TYPE_MONETARY;
10796  break;
10797  default:
10798  /* badly formed expression */
10799  common_type = PT_TYPE_NONE;
10800  break;
10801  }
10802  break;
10803 
10804  case PT_TYPE_NUMERIC:
10805  switch (arg2_type)
10806  {
10807  case PT_TYPE_NUMERIC:
10808  case PT_TYPE_SMALLINT:
10809  case PT_TYPE_INTEGER:
10810  case PT_TYPE_BIGINT:
10811  case PT_TYPE_LOGICAL:
10812  case PT_TYPE_ENUMERATION:
10813  common_type = PT_TYPE_NUMERIC;
10814  break;
10815  case PT_TYPE_FLOAT:
10816  case PT_TYPE_DOUBLE:
10817  common_type = PT_TYPE_DOUBLE;
10818  break;
10819  case PT_TYPE_MONETARY:
10820  common_type = PT_TYPE_MONETARY;
10821  break;
10822  default:
10823  common_type = PT_TYPE_NONE;
10824  break;
10825  }
10826  break;
10827 
10828  case PT_TYPE_FLOAT:
10829  switch (arg2_type)
10830  {
10831  case PT_TYPE_SMALLINT:
10832  case PT_TYPE_INTEGER:
10833  case PT_TYPE_BIGINT:
10834  case PT_TYPE_FLOAT:
10835  case PT_TYPE_LOGICAL:
10836  case PT_TYPE_ENUMERATION:
10837  common_type = PT_TYPE_FLOAT;
10838  break;
10839  case PT_TYPE_DOUBLE:
10840  case PT_TYPE_NUMERIC:
10841  common_type = PT_TYPE_DOUBLE;
10842  break;
10843  case PT_TYPE_MONETARY:
10844  common_type = PT_TYPE_MONETARY;
10845  break;
10846  default:
10847  common_type = PT_TYPE_NONE;
10848  break;
10849  }
10850  break;
10851 
10852  case PT_TYPE_INTEGER:
10853  switch (arg2_type)
10854  {
10855  case PT_TYPE_SMALLINT:
10856  case PT_TYPE_INTEGER:
10857  case PT_TYPE_LOGICAL:
10858  case PT_TYPE_ENUMERATION:
10859  common_type = PT_TYPE_INTEGER;
10860  break;
10861  case PT_TYPE_BIGINT:
10862  case PT_TYPE_FLOAT:
10863  case PT_TYPE_DOUBLE:
10864  case PT_TYPE_DATE:
10865  case PT_TYPE_MONETARY:
10866  case PT_TYPE_TIMESTAMP:
10867  case PT_TYPE_TIMESTAMPLTZ:
10868  case PT_TYPE_TIMESTAMPTZ:
10869  case PT_TYPE_DATETIME:
10870  case PT_TYPE_DATETIMELTZ:
10871  case PT_TYPE_DATETIMETZ:
10872  case PT_TYPE_TIME:
10873  case PT_TYPE_NUMERIC:
10874  common_type = arg2_type;
10875  break;
10876  default:
10877  common_type = PT_TYPE_NONE;
10878  break;
10879  }
10880  break;
10881 
10882  case PT_TYPE_SMALLINT:
10883  switch (arg2_type)
10884  {
10885  case PT_TYPE_SMALLINT:
10886  case PT_TYPE_LOGICAL:
10887  case PT_TYPE_ENUMERATION:
10888  common_type = PT_TYPE_SMALLINT;
10889  break;
10890  case PT_TYPE_INTEGER:
10891  case PT_TYPE_BIGINT:
10892  case PT_TYPE_FLOAT:
10893  case PT_TYPE_DOUBLE:
10894  case PT_TYPE_DATE:
10895  case PT_TYPE_MONETARY:
10896  case PT_TYPE_TIMESTAMP:
10897  case PT_TYPE_TIMESTAMPLTZ:
10898  case PT_TYPE_TIMESTAMPTZ:
10899  case PT_TYPE_DATETIME:
10900  case PT_TYPE_DATETIMELTZ:
10901  case PT_TYPE_DATETIMETZ:
10902  case PT_TYPE_TIME:
10903  case PT_TYPE_NUMERIC:
10904  common_type = arg2_type;
10905  break;
10906  default:
10907  common_type = PT_TYPE_NONE;
10908  break;
10909  }
10910  break;
10911 
10912  case PT_TYPE_BIGINT:
10913  switch (arg2_type)
10914  {
10915  case PT_TYPE_SMALLINT:
10916  case PT_TYPE_INTEGER:
10917  case PT_TYPE_BIGINT:
10918  case PT_TYPE_LOGICAL:
10919  case PT_TYPE_ENUMERATION:
10920  common_type = PT_TYPE_BIGINT;
10921  break;
10922  case PT_TYPE_FLOAT:
10923  common_type = PT_TYPE_FLOAT;
10924  break;
10925  case PT_TYPE_DOUBLE:
10926  common_type = PT_TYPE_DOUBLE;
10927  break;
10928  case PT_TYPE_DATE:
10929  case PT_TYPE_MONETARY:
10930  case PT_TYPE_TIMESTAMP:
10931  case PT_TYPE_TIMESTAMPLTZ:
10932  case PT_TYPE_TIMESTAMPTZ:
10933  case PT_TYPE_DATETIME:
10934  case PT_TYPE_DATETIMELTZ:
10935  case PT_TYPE_DATETIMETZ:
10936  case PT_TYPE_TIME:
10937  case PT_TYPE_NUMERIC:
10938  common_type = arg2_type;
10939  break;
10940  default:
10941  common_type = PT_TYPE_NONE;
10942  break;
10943  }
10944  break;
10945 
10946  case PT_TYPE_MONETARY:
10947  switch (arg2_type)
10948  {
10949  case PT_TYPE_MONETARY:
10950  case PT_TYPE_SMALLINT:
10951  case PT_TYPE_INTEGER:
10952  case PT_TYPE_BIGINT:
10953  case PT_TYPE_FLOAT:
10954  case PT_TYPE_DOUBLE:
10955  case PT_TYPE_NUMERIC:
10956  case PT_TYPE_LOGICAL:
10957  case PT_TYPE_ENUMERATION:
10958  common_type = PT_TYPE_MONETARY;
10959  break;
10960  default:
10961  common_type = PT_TYPE_NONE;
10962  break;
10963  }
10964  break;
10965 
10966  case PT_TYPE_DATETIME:
10967  switch (arg2_type)
10968  {
10969  case PT_TYPE_SMALLINT:
10970  case PT_TYPE_INTEGER:
10971  case PT_TYPE_BIGINT:
10972  case PT_TYPE_CHAR:
10973  case PT_TYPE_VARCHAR:
10974  case PT_TYPE_ENUMERATION:
10975  case PT_TYPE_NCHAR:
10976  case PT_TYPE_VARNCHAR:
10977  case PT_TYPE_DATETIME:
10978  case PT_TYPE_TIMESTAMP:
10979  case PT_TYPE_DATE:
10980  common_type = PT_TYPE_DATETIME;
10981  break;
10982  case PT_TYPE_TIMESTAMPLTZ:
10983  case PT_TYPE_DATETIMELTZ:
10984  common_type = PT_TYPE_DATETIMELTZ;
10985  break;
10986  case PT_TYPE_TIMESTAMPTZ:
10987  case PT_TYPE_DATETIMETZ:
10988  common_type = PT_TYPE_DATETIMETZ;
10989  break;
10990  default:
10991  common_type = PT_TYPE_NONE;
10992  break;
10993  }
10994  break;
10995 
10996  case PT_TYPE_DATETIMELTZ:
10997  switch (arg2_type)
10998  {
10999  case PT_TYPE_SMALLINT:
11000  case PT_TYPE_INTEGER:
11001  case PT_TYPE_BIGINT:
11002  case PT_TYPE_CHAR:
11003  case PT_TYPE_VARCHAR:
11004  case PT_TYPE_ENUMERATION:
11005  case PT_TYPE_NCHAR:
11006  case PT_TYPE_VARNCHAR:
11007  case PT_TYPE_DATETIME:
11008  case PT_TYPE_TIMESTAMP:
11009  case PT_TYPE_DATE:
11010  case PT_TYPE_TIMESTAMPLTZ:
11011  case PT_TYPE_DATETIMELTZ:
11012  common_type = PT_TYPE_DATETIMELTZ;
11013  break;
11014  case PT_TYPE_TIMESTAMPTZ:
11015  case PT_TYPE_DATETIMETZ:
11016  common_type = PT_TYPE_DATETIMETZ;
11017  break;
11018  default:
11019  common_type = PT_TYPE_NONE;
11020  break;
11021  }
11022  break;
11023 
11024  case PT_TYPE_DATETIMETZ:
11025  switch (arg2_type)
11026  {
11027  case PT_TYPE_SMALLINT:
11028  case PT_TYPE_INTEGER:
11029  case PT_TYPE_BIGINT:
11030  case PT_TYPE_CHAR:
11031  case PT_TYPE_VARCHAR:
11032  case PT_TYPE_ENUMERATION:
11033  case PT_TYPE_NCHAR:
11034  case PT_TYPE_VARNCHAR:
11035  case PT_TYPE_DATETIME:
11036  case PT_TYPE_DATETIMELTZ:
11037  case PT_TYPE_TIMESTAMP:
11038  case PT_TYPE_TIMESTAMPLTZ:
11039  case PT_TYPE_TIMESTAMPTZ:
11040  case PT_TYPE_DATE:
11041  common_type = PT_TYPE_DATETIMETZ;
11042  break;
11043 
11044  default:
11045  common_type = PT_TYPE_NONE;
11046  break;
11047  }
11048  break;
11049 
11050  case PT_TYPE_TIMESTAMP:
11051  switch (arg2_type)
11052  {
11053  case PT_TYPE_SMALLINT:
11054  case PT_TYPE_INTEGER:
11055  case PT_TYPE_BIGINT:
11057  {
11058  common_type = PT_TYPE_TIMESTAMP;
11059  }
11060  else
11061  {
11062  common_type = PT_TYPE_BIGINT;
11063  }
11064  break;
11065 
11066  case PT_TYPE_CHAR:
11067  case PT_TYPE_VARCHAR:
11068  case PT_TYPE_NCHAR:
11069  case PT_TYPE_VARNCHAR:
11070  case PT_TYPE_ENUMERATION:
11071  case PT_TYPE_TIMESTAMP:
11072  case PT_TYPE_DATE:
11073  common_type = PT_TYPE_TIMESTAMP;
11074  break;
11075  case PT_TYPE_DATETIME:
11076  case PT_TYPE_DATETIMELTZ:
11077  case PT_TYPE_DATETIMETZ:
11078  case PT_TYPE_TIMESTAMPLTZ:
11079  case PT_TYPE_TIMESTAMPTZ:
11080  common_type = arg2_type;
11081  break;
11082  default:
11083  common_type = PT_TYPE_NONE;
11084  break;
11085  }
11086  break;
11087 
11088  case PT_TYPE_TIMESTAMPLTZ:
11089  switch (arg2_type)
11090  {
11091  case PT_TYPE_SMALLINT:
11092  case PT_TYPE_INTEGER:
11093  case PT_TYPE_BIGINT:
11095  {
11096  common_type = PT_TYPE_TIMESTAMPLTZ;
11097  }
11098  else
11099  {
11100  common_type = PT_TYPE_BIGINT;
11101  }
11102  break;
11103 
11104  case PT_TYPE_CHAR:
11105  case PT_TYPE_VARCHAR:
11106  case PT_TYPE_NCHAR:
11107  case PT_TYPE_VARNCHAR:
11108  case PT_TYPE_ENUMERATION:
11109  case PT_TYPE_TIMESTAMP:
11110  case PT_TYPE_DATE:
11111  common_type = PT_TYPE_TIMESTAMPLTZ;
11112  break;
11113  case PT_TYPE_DATETIME:
11114  case PT_TYPE_DATETIMELTZ:
11115  common_type = PT_TYPE_DATETIMELTZ;
11116  break;
11117  case PT_TYPE_DATETIMETZ:
11118  case PT_TYPE_TIMESTAMPLTZ:
11119  case PT_TYPE_TIMESTAMPTZ:
11120  common_type = arg2_type;
11121  break;
11122  default:
11123  common_type = PT_TYPE_NONE;
11124  break;
11125  }
11126  break;
11127 
11128  case PT_TYPE_TIMESTAMPTZ:
11129  switch (arg2_type)
11130  {
11131  case PT_TYPE_SMALLINT:
11132  case PT_TYPE_INTEGER:
11133  case PT_TYPE_BIGINT:
11135  {
11136  common_type = PT_TYPE_TIMESTAMPTZ;
11137  }
11138  else
11139  {
11140  common_type = PT_TYPE_BIGINT;
11141  }
11142  break;
11143 
11144  case PT_TYPE_CHAR:
11145  case PT_TYPE_VARCHAR:
11146  case PT_TYPE_NCHAR:
11147  case PT_TYPE_VARNCHAR:
11148  case PT_TYPE_ENUMERATION:
11149  case PT_TYPE_TIMESTAMP:
11150  case PT_TYPE_TIMESTAMPLTZ:
11151  case PT_TYPE_TIMESTAMPTZ:
11152  case PT_TYPE_DATE:
11153  common_type = PT_TYPE_TIMESTAMPTZ;
11154  break;
11155  case PT_TYPE_DATETIME:
11156  case PT_TYPE_DATETIMELTZ:
11157  case PT_TYPE_DATETIMETZ:
11158  common_type = PT_TYPE_DATETIMETZ;
11159  break;
11160  default:
11161  common_type = PT_TYPE_NONE;
11162  break;
11163  }
11164  break;
11165 
11166  case PT_TYPE_TIME:
11167  switch (arg2_type)
11168  {
11169  case PT_TYPE_SMALLINT:
11170  case PT_TYPE_INTEGER:
11171  case PT_TYPE_BIGINT:
11173  {
11174  common_type = PT_TYPE_TIME;
11175  }
11176  else
11177  {
11178  common_type = arg2_type;
11179  }
11180  break;
11181  case PT_TYPE_CHAR:
11182  case PT_TYPE_VARCHAR:
11183  case PT_TYPE_NCHAR:
11184  case PT_TYPE_VARNCHAR:
11185  case PT_TYPE_ENUMERATION:
11186  case PT_TYPE_TIME:
11187  common_type = PT_TYPE_TIME;
11188  break;
11189  default:
11190  common_type = PT_TYPE_NONE;
11191  break;
11192  }
11193  break;
11194 
11195  case PT_TYPE_DATE:
11196  switch (arg2_type)
11197  {
11198  case PT_TYPE_SMALLINT:
11199  case PT_TYPE_INTEGER:
11200  case PT_TYPE_BIGINT:
11202  {
11203  common_type = PT_TYPE_DATE;
11204  }
11205  else
11206  {
11207  common_type = arg2_type;
11208  }
11209  break;
11210 
11211  case PT_TYPE_VARCHAR:
11212  case PT_TYPE_CHAR:
11213  case PT_TYPE_VARNCHAR:
11214  case PT_TYPE_NCHAR:
11215  case PT_TYPE_ENUMERATION:
11216  case PT_TYPE_DATE:
11217  common_type = PT_TYPE_DATE;
11218  break;
11219  case PT_TYPE_TIMESTAMP:
11220  case PT_TYPE_TIMESTAMPLTZ:
11221  case PT_TYPE_TIMESTAMPTZ:
11222  case PT_TYPE_DATETIME:
11223  case PT_TYPE_DATETIMELTZ:
11224  case PT_TYPE_DATETIMETZ:
11225  common_type = arg2_type;
11226  break;
11227 
11228  default:
11229  common_type = PT_TYPE_NONE;
11230  break;
11231  }
11232  break;
11233  case PT_TYPE_CHAR:
11234  switch (arg2_type)
11235  {
11236  case PT_TYPE_DATE:
11237  case PT_TYPE_TIME:
11238  case PT_TYPE_TIMESTAMP:
11239  case PT_TYPE_TIMESTAMPLTZ:
11240  case PT_TYPE_TIMESTAMPTZ:
11241  case PT_TYPE_DATETIME:
11242  case PT_TYPE_DATETIMELTZ:
11243  case PT_TYPE_DATETIMETZ:
11244  case PT_TYPE_VARCHAR:
11245  case PT_TYPE_CHAR:
11246  common_type = arg2_type;
11247  break;
11248  case PT_TYPE_ENUMERATION:
11249  common_type = PT_TYPE_VARCHAR;
11250  break;
11251  default:
11252  common_type = PT_TYPE_NONE;
11253  break;
11254  }
11255  break;
11256 
11257  case PT_TYPE_VARCHAR:
11258  switch (arg2_type)
11259  {
11260  case PT_TYPE_DATE:
11261  case PT_TYPE_TIME:
11262  case PT_TYPE_TIMESTAMP:
11263  case PT_TYPE_TIMESTAMPLTZ:
11264  case PT_TYPE_TIMESTAMPTZ:
11265  case PT_TYPE_DATETIME:
11266  case PT_TYPE_DATETIMELTZ:
11267  case PT_TYPE_DATETIMETZ:
11268  case PT_TYPE_VARCHAR:
11269  common_type = arg2_type;
11270  break;
11271  case PT_TYPE_CHAR:
11272  case PT_TYPE_ENUMERATION:
11273  common_type = PT_TYPE_VARCHAR;
11274  break;
11275  default:
11276  common_type = PT_TYPE_NONE;
11277  break;
11278  }
11279  break;
11280 
11281  case PT_TYPE_NCHAR:
11282  switch (arg2_type)
11283  {
11284  case PT_TYPE_DATE:
11285  case PT_TYPE_TIME:
11286  case PT_TYPE_TIMESTAMP:
11287  case PT_TYPE_TIMESTAMPLTZ:
11288  case PT_TYPE_TIMESTAMPTZ:
11289  case PT_TYPE_DATETIME:
11290  case PT_TYPE_DATETIMELTZ:
11291  case PT_TYPE_DATETIMETZ:
11292  case PT_TYPE_NCHAR:
11293  case PT_TYPE_VARNCHAR:
11294  common_type = arg2_type;
11295  break;
11296  default:
11297  common_type = PT_TYPE_NONE;
11298  break;
11299  }
11300  break;
11301 
11302  case PT_TYPE_VARNCHAR:
11303  switch (arg2_type)
11304  {
11305  case PT_TYPE_DATE:
11306  case PT_TYPE_TIME:
11307  case PT_TYPE_TIMESTAMP:
11308  case PT_TYPE_TIMESTAMPLTZ:
11309  case PT_TYPE_TIMESTAMPTZ:
11310  case PT_TYPE_DATETIME:
11311  case PT_TYPE_DATETIMELTZ:
11312  case PT_TYPE_DATETIMETZ:
11313  case PT_TYPE_VARNCHAR:
11314  common_type = arg2_type;
11315  break;
11316  case PT_TYPE_NCHAR:
11317  common_type = PT_TYPE_VARNCHAR;
11318  break;
11319  default:
11320  common_type = PT_TYPE_NONE;
11321  break;
11322  }
11323  break;
11324 
11325  case PT_TYPE_VARBIT:
11326  switch (arg2_type)
11327  {
11328  case PT_TYPE_VARBIT:
11329  case PT_TYPE_BIT:
11330  common_type = PT_TYPE_VARBIT;
11331  break;
11332  default:
11333  common_type = PT_TYPE_NONE;
11334  break;
11335  }
11336  break;
11337 
11338  case PT_TYPE_BIT:
11339  switch (arg2_type)
11340  {
11341  case PT_TYPE_BIT:
11342  common_type = PT_TYPE_BIT;
11343  break;
11344  case PT_TYPE_VARBIT:
11345  common_type = PT_TYPE_VARBIT;
11346  break;
11347  default:
11348  common_type = PT_TYPE_NONE;
11349  break;
11350  }
11351  break;
11352 
11353  case PT_TYPE_OBJECT:
11354  switch (arg2_type)
11355  {
11356  case PT_TYPE_OBJECT:
11357  common_type = PT_TYPE_OBJECT;
11358  break;
11359  default:
11360  common_type = PT_TYPE_NONE;
11361  break;
11362  }
11363  break;
11364 
11365  case PT_TYPE_SET:
11366  case PT_TYPE_MULTISET:
11367  case PT_TYPE_SEQUENCE:
11368  switch (arg2_type)
11369  {
11370  case PT_TYPE_SET:
11371  case PT_TYPE_MULTISET:
11372  case PT_TYPE_SEQUENCE:
11373  common_type = PT_TYPE_MULTISET;
11374  break;
11375  default:
11376  common_type = PT_TYPE_NONE;
11377  break;
11378  }
11379  break;
11380 
11381  case PT_TYPE_LOGICAL:
11382  switch (arg2_type)
11383  {
11384  case PT_TYPE_LOGICAL:
11385  common_type = PT_TYPE_LOGICAL;
11386  break;
11387  case PT_TYPE_DOUBLE:
11388  common_type = PT_TYPE_DOUBLE;
11389  break;
11390  case PT_TYPE_NUMERIC:
11391  common_type = PT_TYPE_NUMERIC;
11392  break;
11393  case PT_TYPE_FLOAT:
11394  common_type = PT_TYPE_FLOAT;
11395  break;
11396  case PT_TYPE_INTEGER:
11397  common_type = PT_TYPE_INTEGER;
11398  break;
11399  case PT_TYPE_SMALLINT:
11400  common_type = PT_TYPE_SMALLINT;
11401  break;
11402  case PT_TYPE_BIGINT:
11403  common_type = PT_TYPE_BIGINT;
11404  break;
11405  case PT_TYPE_MONETARY:
11406  common_type = PT_TYPE_MONETARY;
11407  break;
11408  default:
11409  common_type = PT_TYPE_NONE;
11410  break;
11411  }
11412  break;
11413 
11414  case PT_TYPE_ENUMERATION:
11415  switch (arg2_type)
11416  {
11417  case PT_TYPE_SMALLINT:
11418  case PT_TYPE_INTEGER:
11419  case PT_TYPE_FLOAT:
11420  case PT_TYPE_BIGINT:
11421  case PT_TYPE_NUMERIC:
11422  case PT_TYPE_DOUBLE:
11423  case PT_TYPE_MONETARY:
11424  case PT_TYPE_VARCHAR:
11425  case PT_TYPE_ENUMERATION:
11426  common_type = arg2_type;
11427  break;
11428  case PT_TYPE_CHAR:
11429  common_type = PT_TYPE_VARCHAR;
11430  break;
11431  default:
11432  common_type = pt_common_type (PT_TYPE_VARCHAR, arg2_type);
11433  break;
11434  }
11435  break;
11436 
11437  default:
11438  common_type = PT_TYPE_NONE;
11439  break;
11440  }
11441  }
11442 
11443  if (common_type == PT_TYPE_NONE)
11444  {
11445  if (arg1_type == PT_TYPE_MAYBE || arg2_type == PT_TYPE_MAYBE)
11446  {
11447  common_type = PT_TYPE_MAYBE;
11448  }
11449  else if (arg1_type == PT_TYPE_NA || arg1_type == PT_TYPE_NULL)
11450  {
11451  common_type = arg2_type;
11452  }
11453  else if (arg2_type == PT_TYPE_NA || arg2_type == PT_TYPE_NULL)
11454  {
11455  common_type = arg1_type;
11456  }
11457  }
11458 
11459  return common_type;
11460 }
11461 
11462 /*
11463  * pt_common_type_op () - return the result type of t1 op t2
11464  * return: returns the result type of t1 op t2
11465  * or PT_TYPE_NONE if no such type exists
11466  * t1(in): a data type
11467  * op(in): a binary operator
11468  * t2(in): a data type
11469  */
11470 
11471 static PT_TYPE_ENUM
11472 pt_common_type_op (PT_TYPE_ENUM t1, PT_OP_TYPE op, PT_TYPE_ENUM t2)
11473 {
11474  PT_TYPE_ENUM result_type;
11475 
11476  if (pt_is_op_hv_late_bind (op) && (t1 == PT_TYPE_MAYBE || t2 == PT_TYPE_MAYBE))
11477  {
11478  result_type = PT_TYPE_MAYBE;
11479  }
11480  else
11481  {
11482  result_type = pt_common_type (t1, t2);
11483  }
11484 
11485  switch (op)
11486  {
11487  case PT_MINUS:
11488  case PT_TIMES:
11489  if (result_type == PT_TYPE_SEQUENCE)
11490  {
11491  result_type = PT_TYPE_MULTISET;
11492  }
11493  else if ((PT_IS_STRING_TYPE (t1) && PT_IS_NUMERIC_TYPE (t2))
11494  || (PT_IS_NUMERIC_TYPE (t1) && PT_IS_STRING_TYPE (t2)))
11495  {
11496  /* + and - have their own way of handling this situation */
11497  return PT_TYPE_NONE;
11498  }
11499  break;
11500  case PT_SUPERSET:
11501  case PT_SUPERSETEQ:
11502  case PT_SUBSET:
11503  case PT_SUBSETEQ:
11504  if (result_type == PT_TYPE_SEQUENCE)
11505  {
11506  result_type = PT_TYPE_NONE;
11507  }
11508  break;
11509  case PT_IFNULL:
11510  if (result_type == PT_TYPE_MAYBE)
11511  {
11512  result_type = PT_TYPE_VARCHAR;
11513  }
11514  break;
11515  case PT_COALESCE:
11516  if (t1 == PT_TYPE_MAYBE)
11517  {
11518  if (t2 == PT_TYPE_MAYBE || t2 == PT_TYPE_NULL)
11519  {
11520  result_type = PT_TYPE_MAYBE;
11521  }
11522  else
11523  {
11524  result_type = t2;
11525  }
11526  }
11527  else if (t2 == PT_TYPE_MAYBE)
11528  {
11529  if (t1 == PT_TYPE_MAYBE || t1 == PT_TYPE_NULL)
11530  {
11531  result_type = PT_TYPE_MAYBE;
11532  }
11533  else
11534  {
11535  result_type = t1;
11536  }
11537  }
11538  else if (PT_IS_DATE_TIME_TYPE (result_type) && (t1 != t2))
11539  {
11540  if (PT_IS_DATE_TIME_TYPE (t1))
11541  {
11542  result_type = (PT_IS_DATE_TIME_TYPE (t2)) ? PT_TYPE_DATETIME : t1;
11543  }
11544  else if (PT_IS_DATE_TIME_TYPE (t2))
11545  {
11546  result_type = (PT_IS_DATE_TIME_TYPE (t1)) ? PT_TYPE_DATETIME : t2;
11547  }
11548  }
11549  break;
11550  case PT_IS_IN:
11551  case PT_IS_NOT_IN:
11552  if (result_type == PT_TYPE_MAYBE)
11553  {
11554  if (t1 != PT_TYPE_MAYBE)
11555  {
11556  result_type = t1;
11557  }
11558  else if (t2 != PT_TYPE_MAYBE)
11559  {
11560  result_type = t2;
11561  }
11562  }
11563  break;
11564  default:
11565  break;
11566  }
11567  /*
11568  * true + true must not be logical.
11569  * Same goes for true*true, (i or j)+(i and j) etc.
11570  * Basic rule: if both operands are logical but the operation is not logical,
11571  * then the resulting type MUST be integer. Otherwise strange things happen
11572  * while generating xasl: predicate expressions with wrong operators.
11573  */
11574  if (t1 == PT_TYPE_LOGICAL && t2 == PT_TYPE_LOGICAL && !pt_is_operator_logical (op))
11575  {
11576  result_type = PT_TYPE_INTEGER;
11577  }
11578 
11579  if (pt_is_comp_op (op) && ((PT_IS_NUMERIC_TYPE (t1) && t2 == PT_TYPE_JSON)
11580  || (t1 == PT_TYPE_JSON && PT_IS_NUMERIC_TYPE (t2))))
11581  {
11582  result_type = PT_TYPE_JSON;
11583  }
11584 
11585  return result_type;
11586 }
11587 
11588 
11589 /*
11590  * pt_upd_domain_info () - preparing a node for a binary operation
11591  * involving a parameterized type
11592  * return: NO_ERROR on success, non-zero for ERROR
11593  * parser(in):
11594  * arg1(in):
11595  * arg2(in):
11596  * op(in):
11597  * common_type(in):
11598  * node(out):
11599  */
11600 
11601 static int
11602 pt_upd_domain_info (PARSER_CONTEXT * parser, PT_NODE * arg1, PT_NODE * arg2, PT_OP_TYPE op, PT_TYPE_ENUM common_type,
11603  PT_NODE * node)
11604 {
11605  int arg1_prec = 0;
11606  int arg1_dec_prec = 0;
11607  int arg1_units = 0;
11608  int arg2_prec = 0;
11609  int arg2_dec_prec = 0;
11610  int arg2_units = 0;
11611  PT_NODE *dt = NULL;
11612  bool do_detect_collation = true;
11614 
11615  if (node->data_type)
11616  { /* node has already been resolved */
11617  return NO_ERROR;
11618  }
11619 
11620  /* Retrieve the domain information for arg1 & arg2 */
11621  if (arg1 && arg1->data_type)
11622  {
11623  arg1_prec = arg1->data_type->info.data_type.precision;
11624  arg1_dec_prec = arg1->data_type->info.data_type.dec_precision;
11625  arg1_units = arg1->data_type->info.data_type.units;
11627  {
11628  collation_flag = TP_DOMAIN_COLL_NORMAL;
11629  }
11630  }
11631  else if (arg1 && arg1->type_enum == PT_TYPE_INTEGER)
11632  {
11633  arg1_prec = TP_INTEGER_PRECISION;
11634  arg1_dec_prec = 0;
11635  arg1_units = 0;
11636  }
11637  else if (arg1 && arg1->type_enum == PT_TYPE_BIGINT)
11638  {
11639  arg1_prec = TP_BIGINT_PRECISION;
11640  arg1_dec_prec = 0;
11641  arg1_units = 0;
11642  }
11643  else if (arg1 && arg1->type_enum == PT_TYPE_SMALLINT)
11644  {
11645  arg1_prec = TP_SMALLINT_PRECISION;
11646  arg1_dec_prec = 0;
11647  arg1_units = 0;
11648  }
11649  else if (arg1 && arg1->type_enum == PT_TYPE_NUMERIC)
11650  {
11651  arg1_prec = DB_DEFAULT_NUMERIC_PRECISION;
11652  arg1_dec_prec = DB_DEFAULT_NUMERIC_SCALE;
11653  arg1_units = 0;
11654  }
11655  else if (arg1 && arg1->type_enum == PT_TYPE_MAYBE)
11656  {
11657  arg1_prec = TP_FLOATING_PRECISION_VALUE;
11658  arg1_dec_prec = 0;
11659  arg1_units = 0;
11660  }
11661  else
11662  {
11663  arg1_prec = 0;
11664  arg1_dec_prec = 0;
11665  arg1_units = 0;
11666  }
11667 
11668  if (arg2 && arg2->data_type)
11669  {
11670  arg2_prec = arg2->data_type->info.data_type.precision;
11671  arg2_dec_prec = arg2->data_type->info.data_type.dec_precision;
11672  arg2_units = arg2->data_type->info.data_type.units;
11674  {
11675  collation_flag = TP_DOMAIN_COLL_NORMAL;
11676  }
11677  }
11678  else if (arg2 && arg2->type_enum == PT_TYPE_INTEGER)
11679  {
11680  arg2_prec = TP_INTEGER_PRECISION;
11681  arg2_dec_prec = 0;
11682  arg2_units = 0;
11683  }
11684  else if (arg2 && arg2->type_enum == PT_TYPE_BIGINT)
11685  {
11686  arg2_prec = TP_BIGINT_PRECISION;
11687  arg2_dec_prec = 0;
11688  arg2_units = 0;
11689  }
11690  else if (arg2 && arg2->type_enum == PT_TYPE_SMALLINT)
11691  {
11692  arg2_prec = TP_SMALLINT_PRECISION;
11693  arg2_dec_prec = 0;
11694  arg2_units = 0;
11695  }
11696  else if (arg2 && arg2->type_enum == PT_TYPE_NUMERIC)
11697  {
11698  arg2_prec = DB_DEFAULT_NUMERIC_PRECISION;
11699  arg2_dec_prec = DB_DEFAULT_NUMERIC_SCALE;
11700  arg2_units = 0;
11701  }
11702  else if (arg2 && arg2->type_enum == PT_TYPE_MAYBE)
11703  {
11704  arg2_prec = TP_FLOATING_PRECISION_VALUE;
11705  arg2_dec_prec = 0;
11706  arg2_units = 0;
11707  }
11708  else
11709  {
11710  arg2_prec = 0;
11711  arg2_dec_prec = 0;
11712  arg2_units = 0;
11713  }
11714 
11715  if (op == PT_MINUS || op == PT_PLUS || op == PT_STRCAT || op == PT_SYS_CONNECT_BY_PATH || op == PT_PRIOR
11716  || op == PT_CONNECT_BY_ROOT || op == PT_QPRIOR || op == PT_UNARY_MINUS || op == PT_FLOOR || op == PT_CEIL
11717  || op == PT_ABS || op == PT_ROUND || op == PT_TRUNC || op == PT_CASE || op == PT_NULLIF || op == PT_COALESCE
11718  || op == PT_NVL || op == PT_NVL2 || op == PT_DECODE || op == PT_LEAST || op == PT_GREATEST || op == PT_CHR
11719  || op == PT_BIT_NOT || op == PT_BIT_AND || op == PT_BIT_OR || op == PT_BIT_XOR || op == PT_BITSHIFT_LEFT
11720  || op == PT_BITSHIFT_RIGHT || op == PT_DIV || op == PT_MOD || op == PT_IF || op == PT_IFNULL || op == PT_CONCAT
11721  || op == PT_CONCAT_WS || op == PT_FIELD || op == PT_UNIX_TIMESTAMP || op == PT_BIT_COUNT || op == PT_REPEAT
11722  || op == PT_SPACE || op == PT_MD5 || op == PT_TIMEF || op == PT_AES_ENCRYPT || op == PT_AES_DECRYPT
11723  || op == PT_SHA_TWO || op == PT_SHA_ONE || op == PT_TO_BASE64 || op == PT_FROM_BASE64 || op == PT_DEFAULTF)
11724  {
11726  if (dt == NULL)
11727  {
11728  return ER_OUT_OF_VIRTUAL_MEMORY;
11729  }
11730  }
11731  else if (common_type == PT_TYPE_NUMERIC && (op == PT_TIMES || op == PT_POWER || op == PT_DIVIDE || op == PT_MODULUS))
11732  {
11734  if (dt == NULL)
11735  {
11736  return ER_OUT_OF_VIRTUAL_MEMORY;
11737  }
11738  }
11739 
11740  switch (op)
11741  {
11742  case PT_MINUS:
11743  case PT_PLUS:
11744  if (arg1_prec == TP_FLOATING_PRECISION_VALUE || arg2_prec == TP_FLOATING_PRECISION_VALUE)
11745  {
11747  dt->info.data_type.dec_precision = 0;
11748  dt->info.data_type.units = 0;
11749  }
11750  else if (common_type == PT_TYPE_NUMERIC)
11751  {
11752  int integral_digits1, integral_digits2;
11753 
11754  integral_digits1 = arg1_prec - arg1_dec_prec;
11755  integral_digits2 = arg2_prec - arg2_dec_prec;
11756  dt->info.data_type.dec_precision = MAX (arg1_dec_prec, arg2_dec_prec);
11757  dt->info.data_type.precision =
11758  (dt->info.data_type.dec_precision + MAX (integral_digits1, integral_digits2) + 1);
11759  dt->info.data_type.units = 0;
11760  }
11761  else
11762  {
11763  dt->info.data_type.precision = arg1_prec + arg2_prec;
11764  dt->info.data_type.dec_precision = 0;
11765  dt->info.data_type.units = arg1_units;
11766  }
11767  break;
11768 
11769  case PT_REPEAT:
11770  {
11772  if (arg1_prec != TP_FLOATING_PRECISION_VALUE && arg2->node_type == PT_VALUE
11773  && arg2->type_enum == PT_TYPE_INTEGER)
11774  {
11775  precision = arg1_prec * arg2->info.value.data_value.i;
11776  }
11778  dt->info.data_type.dec_precision = 0;
11779  dt->info.data_type.units = arg1_units;
11780  }
11781  break;
11782 
11783  case PT_SPACE:
11784  if (arg1->node_type == PT_VALUE)
11785  {
11786  switch (arg1->type_enum)
11787  {
11788  case PT_TYPE_SMALLINT:
11789  if (arg1->info.value.data_value.b < 0)
11790  {
11791  dt->info.data_type.precision = 0;
11792  }
11793  else
11794  {
11796  }
11797  break;
11798  case PT_TYPE_INTEGER:
11800  {
11801  node->type_enum = PT_TYPE_NULL;
11802  }
11803  else
11804  {
11805  if (arg1->info.value.data_value.i < 0)
11806  {
11807  dt->info.data_type.precision = 0;
11808  }
11809  else
11810  {
11812  }
11813  }
11814  break;
11815  case PT_TYPE_BIGINT:
11817  {
11818  node->type_enum = PT_TYPE_NULL;
11819  }
11820  else
11821  {
11822  if (arg1->info.value.data_value.bigint < 0)
11823  {
11824  dt->info.data_type.precision = 0;
11825  }
11826  else
11827  {
11829  }
11830  }
11831  break;
11832  default:
11834  break;
11835  }
11836  }
11837  else
11838  {
11840  }
11841  break;
11842 
11843  case PT_CONCAT:
11844  case PT_CONCAT_WS:
11845  case PT_FIELD:
11846  case PT_STRCAT:
11849  dt->info.data_type.dec_precision = 0;
11850  dt->info.data_type.units = 0;
11851  break;
11852 
11853  case PT_TIMES:
11854  case PT_POWER:
11855  if (common_type == PT_TYPE_NUMERIC)
11856  {
11857  if (arg1_prec == TP_FLOATING_PRECISION_VALUE || arg2_prec == TP_FLOATING_PRECISION_VALUE)
11858  {
11860  dt->info.data_type.dec_precision = 0;
11861  dt->info.data_type.units = 0;
11862  }
11863  else
11864  {
11865  dt->info.data_type.precision = arg1_prec + arg2_prec + 1;
11866  dt->info.data_type.dec_precision = (arg1_dec_prec + arg2_dec_prec);
11867  dt->info.data_type.units = 0;
11868  }
11869  }
11870  break;
11871 
11872  case PT_DIVIDE:
11873  case PT_MODULUS:
11874  if (common_type == PT_TYPE_NUMERIC)
11875  {
11876  if (arg1_prec == TP_FLOATING_PRECISION_VALUE || arg2_prec == TP_FLOATING_PRECISION_VALUE)
11877  {
11879  dt->info.data_type.dec_precision = 0;
11880  dt->info.data_type.units = 0;
11881  }
11882  else
11883  {
11884  int scaleup = 0;
11885 
11886  if (arg2_dec_prec > 0)
11887  {
11888  scaleup = (MAX (arg1_dec_prec, arg2_dec_prec) + arg2_dec_prec - arg1_dec_prec);
11889  }
11890  dt->info.data_type.precision = arg1_prec + scaleup;
11891  dt->info.data_type.dec_precision = ((arg1_dec_prec > arg2_dec_prec) ? arg1_dec_prec : arg2_dec_prec);
11892  dt->info.data_type.units = 0;
11894  {
11896  {
11897  int org_prec, org_scale, new_prec, new_scale;
11898  int scale_delta;
11899 
11900  org_prec = MIN (38, dt->info.data_type.precision);
11901  org_scale = dt->info.data_type.dec_precision;
11902  scale_delta = (DB_DEFAULT_NUMERIC_DIVISION_SCALE - org_scale);
11903  new_scale = org_scale + scale_delta;
11904  new_prec = org_prec + scale_delta;
11905  if (new_prec > DB_MAX_NUMERIC_PRECISION)
11906  {
11907  new_scale -= (new_prec - DB_MAX_NUMERIC_PRECISION);
11908  new_prec = DB_MAX_NUMERIC_PRECISION;
11909  }
11910 
11911  dt->info.data_type.precision = new_prec;
11912  dt->info.data_type.dec_precision = new_scale;
11913  }
11914  }
11915  }
11916  }
11917  break;
11918  case PT_TIMEF:
11919  dt->info.data_type.precision = 12;
11920  dt->info.data_type.dec_precision = 0;
11921  dt->info.data_type.units = 0;
11922  break;
11923 
11924  case PT_UNARY_MINUS:
11925  case PT_FLOOR:
11926  case PT_CEIL:
11927  case PT_ABS:
11928  case PT_ROUND:
11929  case PT_TRUNC:
11930  case PT_PRIOR:
11931  case PT_CONNECT_BY_ROOT:
11932  case PT_QPRIOR:
11933  case PT_BIT_NOT:
11934  case PT_BIT_AND:
11935  case PT_BIT_OR:
11936  case PT_BIT_XOR:
11937  case PT_BIT_COUNT:
11938  case PT_BITSHIFT_LEFT:
11939  case PT_BITSHIFT_RIGHT:
11940  case PT_DIV:
11941  case PT_MOD:
11942  dt->info.data_type.precision = arg1_prec;
11943  dt->info.data_type.dec_precision = arg1_dec_prec;
11944  dt->info.data_type.units = arg1_units;
11945  break;
11946 
11947  case PT_IF:
11948  case PT_IFNULL:
11949  case PT_CASE:
11950  case PT_NULLIF:
11951  case PT_COALESCE:
11952  case PT_NVL:
11953  case PT_NVL2:
11954  case PT_DECODE:
11955  case PT_LEAST:
11956  case PT_GREATEST:
11957  if (arg1_prec == TP_FLOATING_PRECISION_VALUE || arg2_prec == TP_FLOATING_PRECISION_VALUE)
11958  {
11960  dt->info.data_type.dec_precision = 0;
11961  dt->info.data_type.units = 0;
11962  }
11963  else if (common_type == PT_TYPE_NUMERIC)
11964  {
11965  int integral_digits1, integral_digits2;
11966 
11967  integral_digits1 = arg1_prec - arg1_dec_prec;
11968  integral_digits2 = arg2_prec - arg2_dec_prec;
11969  dt->info.data_type.dec_precision = MAX (arg1_dec_prec, arg2_dec_prec);
11970  dt->info.data_type.precision = (MAX (integral_digits1, integral_digits2) + dt->info.data_type.dec_precision);
11971  dt->info.data_type.units = 0;
11972  }
11973  else if ((arg1->type_enum != arg2->type_enum) && pt_is_op_with_forced_common_type (op))
11974  {
11976  dt->info.data_type.dec_precision = 0;
11977  dt->info.data_type.units = 0;
11978  }
11979  else
11980  {
11981  dt->info.data_type.precision = MAX (arg1_prec, arg2_prec);
11982  dt->info.data_type.dec_precision = 0;
11983  if (arg1 && arg1->type_enum == common_type)
11984  {
11985  dt->info.data_type.units = arg1_units;
11986  }
11987  else
11988  {
11989  dt->info.data_type.units = arg2_units;
11990  }
11991  }
11992  break;
11993 
11994  case PT_BIT_TO_BLOB:
11995  case PT_CHAR_TO_BLOB:
11996  case PT_BLOB_FROM_FILE:
11997  assert (dt == NULL);
11999  if (dt == NULL)
12000  {
12001  return ER_OUT_OF_VIRTUAL_MEMORY;
12002  }
12003  break;
12004 
12005  case PT_BLOB_LENGTH:
12006  case PT_CLOB_LENGTH:
12007  assert (dt == NULL);
12009  if (dt == NULL)
12010  {
12011  return ER_OUT_OF_VIRTUAL_MEMORY;
12012  }
12013  break;
12014 
12015  case PT_BLOB_TO_BIT:
12016  assert (dt == NULL);
12018  if (dt == NULL)
12019  {
12020  return ER_OUT_OF_VIRTUAL_MEMORY;
12021  }
12022  break;
12023  case PT_CLOB_TO_CHAR:
12024  assert (dt == NULL);
12026  if (dt == NULL)
12027  {
12028  return ER_OUT_OF_VIRTUAL_MEMORY;
12029  }
12030  if (arg2 != NULL)
12031  {
12032  assert (arg2->node_type == PT_VALUE);
12033  assert (arg2->type_enum == PT_TYPE_INTEGER);
12034  dt->info.data_type.units = arg2->info.value.data_value.i;
12036  do_detect_collation = false;
12037  }
12038  break;
12039 
12040 
12041  case PT_CHAR_TO_CLOB:
12042  case PT_CLOB_FROM_FILE:
12043  assert (dt == NULL);
12045  if (dt == NULL)
12046  {
12047  return ER_OUT_OF_VIRTUAL_MEMORY;
12048  }
12049  break;
12050  case PT_LIST_DBS:
12051  case PT_TO_CHAR:
12052  case PT_USER:
12053  case PT_DATABASE:
12054  case PT_SCHEMA:
12055  case PT_CURRENT_USER:
12056  case PT_INET_NTOA:
12057  case PT_SCHEMA_DEF:
12058  assert (dt == NULL);
12060  if (op != PT_TO_CHAR)
12061  {
12062  do_detect_collation = false;
12063  }
12064  break;
12065  case PT_SUBSTRING_INDEX:
12066  assert (dt == NULL);
12067  dt = parser_copy_tree_list (parser, arg1->data_type);
12068  break;
12069  case PT_VERSION:
12070  assert (dt == NULL);
12072  /* Set a large enough precision so that CUBRID is able to handle versions like 'xxx.xxx.xxx.xxxx'. */
12073  dt->info.data_type.precision = 16;
12074  break;
12075  case PT_SYS_GUID:
12076  assert (dt == NULL);
12078  /* Set a large enough precision so that CUBRID is able to handle GUID like 'B5B2D2FA9633460F820589FFDBD8C309'. */
12079  dt->info.data_type.precision = 32;
12080  break;
12081  case PT_CHR:
12082  assert (dt != NULL);
12084  if (arg2 != NULL)
12085  {
12086  assert (arg2->node_type == PT_VALUE);
12087  assert (arg2->type_enum == PT_TYPE_INTEGER);
12088  dt->info.data_type.units = arg2->info.value.data_value.i;
12090  do_detect_collation = false;
12091  }
12092  break;
12093 
12094  case PT_MD5:
12095  assert (dt != NULL);
12096  dt->info.data_type.precision = 32;
12097  break;
12098 
12099  case PT_AES_ENCRYPT:
12100  case PT_AES_DECRYPT:
12101  case PT_SHA_TWO:
12102  assert (dt != NULL);
12104  dt->info.data_type.dec_precision = 0;
12105  dt->info.data_type.units = 0;
12106  break;
12107 
12108  case PT_SHA_ONE:
12109  assert (dt != NULL);
12110  dt->info.data_type.precision = 40;
12111  break;
12112 
12113  case PT_TO_BASE64:
12114  case PT_FROM_BASE64:
12115  assert (dt != NULL);
12117  break;
12118 
12119  case PT_LAST_INSERT_ID:
12120  assert (dt == NULL);
12121  /* last insert id returns NUMERIC (38, 0) */
12124  dt->info.data_type.dec_precision = 0;
12125  break;
12126 
12127  case PT_BIN:
12128  assert (dt == NULL);
12129  dt = pt_make_prim_data_type (parser, node->type_enum);
12130  if (dt == NULL)
12131  {
12132  break;
12133  }
12134  /* bin returns the binary representation of a BIGINT */
12135  dt->info.data_type.precision = (sizeof (DB_BIGINT) * 8);
12136  do_detect_collation = false;
12137  break;
12138 
12139  case PT_ADDTIME:
12140  if (node->type_enum == PT_TYPE_VARCHAR)
12141  {
12142  assert (dt == NULL);
12143  dt = pt_make_prim_data_type (parser, node->type_enum);
12144  if (dt == NULL)
12145  {
12146  break;
12147  }
12148  /* holds at most a DATETIME type */
12150  }
12151  break;
12152 
12153  case PT_CONV:
12154  if (PT_IS_STRING_TYPE (node->type_enum))
12155  {
12156  assert (dt == NULL);
12157  dt = pt_make_prim_data_type (parser, node->type_enum);
12159  }
12160  do_detect_collation = false;
12161  break;
12162 
12163  case PT_CHARSET:
12164  case PT_COLLATION:
12165  case PT_FORMAT:
12166  case PT_TYPEOF:
12167  case PT_HEX:
12168  do_detect_collation = false;
12169  case PT_TRIM:
12170  case PT_LTRIM:
12171  case PT_RTRIM:
12172  case PT_MID:
12173  case PT_REPLACE:
12174  case PT_TRANSLATE:
12175  case PT_TIME_FORMAT:
12176  case PT_LPAD:
12177  case PT_RPAD:
12178  case PT_SUBSTRING:
12179  case PT_COERCIBILITY:
12180  case PT_INDEX_PREFIX:
12181  assert (dt == NULL);
12182  dt = pt_make_prim_data_type (parser, node->type_enum);
12184  break;
12185 
12186  case PT_FUNCTION_HOLDER:
12187  if (node->info.function.function_type == F_ELT || node->info.function.function_type == F_INSERT_SUBSTRING
12188  || node->info.function.function_type == F_JSON_OBJECT || node->info.function.function_type == F_JSON_ARRAY
12189  || node->info.function.function_type == F_JSON_INSERT || node->info.function.function_type == F_JSON_REMOVE
12190  || node->info.function.function_type == F_JSON_MERGE
12191  || node->info.function.function_type == F_JSON_ARRAY_APPEND
12192  || node->info.function.function_type == F_JSON_GET_ALL_PATHS
12193  || node->info.function.function_type == F_JSON_REPLACE || node->info.function.function_type == F_JSON_SET
12194  || node->info.function.function_type == F_JSON_KEYS)
12195  {
12196  assert (dt == NULL);
12197  dt = pt_make_prim_data_type (parser, node->type_enum);
12199  }
12200  break;
12201 
12202  case PT_SUBDATE:
12203  case PT_ADDDATE:
12204  case PT_DATE_SUB:
12205  case PT_DATE_ADD:
12206  case PT_DATEF:
12207  if (PT_IS_STRING_TYPE (node->type_enum))
12208  {
12209  assert (dt == NULL);
12210  dt = pt_make_prim_data_type (parser, node->type_enum);
12211  dt->info.data_type.precision = 32;
12212  }
12213  break;
12214 
12215  case PT_FROM_UNIXTIME:
12216  case PT_DATE_FORMAT:
12217  if (PT_IS_STRING_TYPE (node->type_enum))
12218  {
12219  assert (dt == NULL);
12220  dt = pt_make_prim_data_type (parser, node->type_enum);
12222  }
12223  break;
12224 
12225  case PT_LOWER:
12226  case PT_UPPER:
12227  case PT_REVERSE:
12228  case PT_LEFT:
12229  case PT_RIGHT:
12230  assert (dt == NULL);
12231  if (PT_IS_HOSTVAR (arg1))
12232  {
12233  /* we resolved the node type to a variable char type (VARCHAR orVARNCHAR) but we have to set an unknown
12234  * precision here */
12235  dt = pt_make_prim_data_type (parser, node->type_enum);
12237  }
12238  else
12239  {
12240  dt = parser_copy_tree_list (parser, arg1->data_type);
12241  }
12242  break;
12243 
12244  case PT_TO_NUMBER:
12245  {
12246  int prec = 0, scale = 0;
12247  pt_to_regu_resolve_domain (&prec, &scale, arg2);
12249  break;
12250  }
12251 
12252  case PT_EXEC_STATS:
12253  assert (dt == NULL);
12255  break;
12256 
12257  case PT_TZ_OFFSET:
12258  assert (dt == NULL);
12259  dt = pt_make_prim_data_type (parser, node->type_enum);
12263  break;
12264 
12265  case PT_TRACE_STATS:
12266  assert (dt == NULL);
12268  do_detect_collation = false;
12269  break;
12270 
12271  case PT_CRC32:
12272  case PT_DISK_SIZE:
12273  assert (dt == NULL);
12275  break;
12276 
12277  case PT_DEFAULTF:
12278  dt = parser_copy_tree_list (parser, arg1->data_type);
12279  break;
12280 
12281  default:
12282  break;
12283  }
12284 
12285  if (dt)
12286  {
12287  /* in any case the precision can't be greater than the max precision for the result. */
12288  switch (common_type)
12289  {
12290  case PT_TYPE_CHAR:
12293  break;
12294 
12295  case PT_TYPE_VARCHAR:
12298  break;
12299 
12300  case PT_TYPE_NCHAR:
12303  break;
12304 
12305  case PT_TYPE_VARNCHAR:
12308  break;
12309 
12310  case PT_TYPE_BIT:
12313  break;
12314 
12315  case PT_TYPE_VARBIT:
12318  break;
12319 
12320  case PT_TYPE_NUMERIC:
12322  {
12325  }
12326 
12329  break;
12330 
12331  case PT_TYPE_ENUMERATION:
12332  if (arg1 != NULL && arg1->type_enum == PT_TYPE_ENUMERATION)
12333  {
12334  dt = parser_copy_tree_list (parser, arg1->data_type);
12335  }
12336  else if (arg2 != NULL && arg2->type_enum == PT_TYPE_ENUMERATION)
12337  {
12338  dt = parser_copy_tree_list (parser, arg2->data_type);
12339  }
12340  break;
12341 
12342  default:
12343  break;
12344  }
12345 
12346  /* Basic collation inference, add specific code in 'pt_check_expr_collation' */
12347  if (do_detect_collation && PT_HAS_COLLATION (common_type))
12348  {
12349  if (arg1 != NULL && PT_HAS_COLLATION (arg1->type_enum) && arg1->data_type != NULL
12350  && (arg2 == NULL || (arg2 != NULL && !PT_HAS_COLLATION (arg2->type_enum))))
12351  {
12354  }
12355  else if (arg2 != NULL && PT_HAS_COLLATION (arg2->type_enum) && arg2->data_type != NULL
12356  && (arg1 == NULL || (arg1 != NULL && !PT_HAS_COLLATION (arg1->type_enum))))
12357  {
12360  }
12361  else
12362  {
12365  if ((arg1 == NULL || arg1->type_enum != PT_TYPE_MAYBE)
12366  && (arg2 == NULL || arg2->type_enum != PT_TYPE_MAYBE)
12367  && (!((PT_NODE_IS_SESSION_VARIABLE (arg1)) && (PT_NODE_IS_SESSION_VARIABLE (arg2)))))
12368  {
12369  /* operator without arguments or with arguments has result with system collation */
12370  collation_flag = TP_DOMAIN_COLL_NORMAL;
12371  }
12372  }
12373  dt->info.data_type.collation_flag = collation_flag;
12374  }
12375  node->data_type = dt;
12376  node->data_type->type_enum = common_type;
12377  }
12378 
12379  return NO_ERROR;
12380 }
12381 
12382 /*
12383  * pt_check_and_coerce_to_time () - check if explicit time format and
12384  * coerce to time
12385  * return: NO_ERROR on success, non-zero for ERROR
12386  * parser(in):
12387  * src(in/out): a pointer to the original PT_VALUE
12388  */
12389 static int
12390 pt_check_and_coerce_to_time (PARSER_CONTEXT * parser, PT_NODE * src)
12391 {
12392  DB_VALUE *db_src = NULL;
12393  char *cp;
12394  DB_TYPE dbtype;
12395  int cp_len;
12396 
12397  db_src = pt_value_to_db (parser, src);
12398  if (!db_src)
12399  {
12400  return ER_TIME_CONVERSION;
12401  }
12402 
12403  dbtype = DB_VALUE_TYPE (db_src);
12405  {
12406  return ER_TIME_CONVERSION;
12407  }
12408 
12409  if (db_get_string (db_src) == NULL)
12410  {
12411  return ER_TIME_CONVERSION;
12412  }
12413 
12414  cp = db_get_string (db_src);
12415  cp_len = db_get_string_size (db_src);
12416  if (db_string_check_explicit_time (cp, cp_len) == false)
12417  {
12418  return ER_TIME_CONVERSION;
12419  }
12420 
12421  return pt_coerce_value (parser, src, src, PT_TYPE_TIME, NULL);
12422 }
12423 
12424 /*
12425  * pt_check_and_coerce_to_date () - check if explicit date format and
12426  * coerce to date
12427  * return: NO_ERROR on success, non-zero for ERROR
12428  * parser(in):
12429  * src(in/out): node to be checked
12430  */
12431 static int
12432 pt_check_and_coerce_to_date (PARSER_CONTEXT * parser, PT_NODE * src)
12433 {
12434  DB_VALUE *db_src = NULL;
12435  char *str = NULL;
12436  int str_len;
12437 
12438  assert (src != NULL);
12439 
12440  if (!PT_IS_CHAR_STRING_TYPE (src->type_enum))
12441  {
12442  return ER_DATE_CONVERSION;
12443  }
12444 
12445  db_src = pt_value_to_db (parser, src);
12446  if (DB_IS_NULL (db_src))
12447  {
12448  return ER_DATE_CONVERSION;
12449  }
12450 
12451  if (db_get_string (db_src) == NULL)
12452  {
12453  return ER_DATE_CONVERSION;
12454  }
12455 
12456  str = db_get_string (db_src);
12457  str_len = db_get_string_size (db_src);
12458  if (!db_string_check_explicit_date (str, str_len))
12459  {
12460  return ER_DATE_CONVERSION;
12461  }
12462  return pt_coerce_value (parser, src, src, PT_TYPE_DATE, NULL);
12463 }
12464 
12465 /*
12466  * pt_coerce_str_to_time_date_utime_datetime () - try to coerce into
12467  * a date, time or utime
12468  * return: NO_ERROR on success, non-zero for ERROR
12469  * parser(in):
12470  * src(in/out): a pointer to the original PT_VALUE
12471  * result_type(out): the result type of the coerced result
12472  */
12473 static int
12474 pt_coerce_str_to_time_date_utime_datetime (PARSER_CONTEXT * parser, PT_NODE * src, PT_TYPE_ENUM * result_type)
12475 {
12476  int result = -1;
12477 
12478  if (src == NULL || !PT_IS_CHAR_STRING_TYPE (src->type_enum) || pt_has_error (parser))
12479  {
12480  return result;
12481  }
12482 
12483  /* try coercing to time */
12484  if (pt_check_and_coerce_to_time (parser, src) == NO_ERROR)
12485  {
12486  *result_type = PT_TYPE_TIME;
12487  result = NO_ERROR;
12488  }
12489  else
12490  {
12491  /* get rid of error msg from previous coercion */
12492  parser_free_tree (parser, parser->error_msgs);
12493  parser->error_msgs = NULL;
12494 
12495  /* try coercing to date */
12496  if (pt_check_and_coerce_to_date (parser, src) == NO_ERROR)
12497  {
12498  *result_type = PT_TYPE_DATE;
12499  result = NO_ERROR;
12500  }
12501  else
12502  {
12503  parser_free_tree (parser, parser->error_msgs);
12504  parser->error_msgs = NULL;
12505 
12506  /* try coercing to utime */
12507  if (pt_coerce_value (parser, src, src, PT_TYPE_TIMESTAMP, NULL) == NO_ERROR)
12508  {
12509  *result_type = PT_TYPE_TIMESTAMP;
12510  result = NO_ERROR;
12511  }
12512  else
12513  {
12514  parser_free_tree (parser, parser->error_msgs);
12515  parser->error_msgs = NULL;
12516 
12517  /* try coercing to datetime */
12518  if (pt_coerce_value (parser, src, src, PT_TYPE_DATETIME, NULL) == NO_ERROR)
12519  {
12520  *result_type = PT_TYPE_DATETIME;
12521  result = NO_ERROR;
12522  }
12523  }
12524  }
12525  }
12526 
12527  return result;
12528 }
12529 
12530 
12531 /*
12532  * pt_coerce_3args () - try to coerce 3 opds into their common_type if any
12533  * return: returns 1 on success, 0 otherwise
12534  * parser(in): the parser context
12535  * arg1(in): 1st operand
12536  * arg2(in): 2nd operand
12537  * arg3(in): 3rd operand
12538  */
12539 
12540 static int
12541 pt_coerce_3args (PARSER_CONTEXT * parser, PT_NODE * arg1, PT_NODE * arg2, PT_NODE * arg3)
12542 {
12543  PT_TYPE_ENUM common_type;
12544  PT_NODE *data_type = NULL;
12545  int result = 1;
12546 
12547  common_type = pt_common_type (arg1->type_enum, pt_common_type (arg2->type_enum, arg3->type_enum));
12548  if (common_type == PT_TYPE_NONE)
12549  {
12550  return 0;
12551  }
12552 
12553  /* try to coerce non-identical args into the common type */
12554  if (PT_IS_NUMERIC_TYPE (common_type) || PT_IS_STRING_TYPE (common_type))
12555  {
12556  data_type = NULL;
12557  }
12558  else
12559  {
12560  if (arg1->type_enum == common_type)
12561  {
12562  data_type = arg1->data_type;
12563  }
12564  else if (arg2->type_enum == common_type)
12565  {
12566  data_type = arg2->data_type;
12567  }
12568  else if (arg3->type_enum == common_type)
12569  {
12570  data_type = arg3->data_type;
12571  }
12572  }
12573 
12574  if (arg1->type_enum != common_type)
12575  {
12576  if (PT_IS_COLLECTION_TYPE (common_type))
12577  {
12578  data_type = arg1->data_type;
12579  }
12580 
12581  result = (result && (pt_coerce_value (parser, arg1, arg1, common_type, data_type) == NO_ERROR));
12582  }
12583 
12584  if (arg2->type_enum != common_type)
12585  {
12586  if (PT_IS_COLLECTION_TYPE (common_type))
12587  {
12588  data_type = arg2->data_type;
12589  }
12590 
12591  result = (result && (pt_coerce_value (parser, arg2, arg2, common_type, data_type) == NO_ERROR));
12592  }
12593 
12594  if (arg3->type_enum != common_type)
12595  {
12596  if (PT_IS_COLLECTION_TYPE (common_type))
12597  {
12598  data_type = arg3->data_type;
12599  }
12600 
12601  result = (result && (pt_coerce_value (parser, arg3, arg3, common_type, data_type) == NO_ERROR));
12602  }
12603 
12604  return result;
12605 }
12606 
12607 /* pt_character_length_for_node() -
12608  return: number of characters that a value of the given type can possibly
12609  occuppy when cast to a CHAR type.
12610  node(in): node with type whose character length is to be returned.
12611  coerce_type(in): string type that node will be cast to
12612 */
12613 static int
12614 pt_character_length_for_node (PT_NODE * node, const PT_TYPE_ENUM coerce_type)
12615 {
12617 
12618  switch (node->type_enum)
12619  {
12620  case PT_TYPE_DOUBLE:
12622  break;
12623  case PT_TYPE_FLOAT:
12625  break;
12626  case PT_TYPE_MONETARY:
12628  break;
12629  case PT_TYPE_BIGINT:
12631  break;
12632  case PT_TYPE_INTEGER:
12634  break;
12635  case PT_TYPE_SMALLINT:
12637  break;
12638  case PT_TYPE_TIME:
12640  break;
12641  case PT_TYPE_DATE:
12643  break;
12644  case PT_TYPE_TIMESTAMP:
12646  break;
12647  case PT_TYPE_TIMESTAMPLTZ:
12648  case PT_TYPE_TIMESTAMPTZ:
12650  break;
12651  case PT_TYPE_DATETIME:
12653  break;
12654  case PT_TYPE_DATETIMETZ:
12655  case PT_TYPE_DATETIMELTZ:
12657  break;
12658  case PT_TYPE_NUMERIC:
12659  if (node->data_type == NULL)
12660  {
12661  precision = DB_DEFAULT_NUMERIC_PRECISION + 1; /* sign */
12662  break;
12663  }
12664 
12665  precision = node->data_type->info.data_type.precision;
12666  if (precision == 0 || precision == DB_DEFAULT_PRECISION)
12667  {
12669  }
12670  precision++; /* for sign */
12671 
12672  if (node->data_type->info.data_type.dec_precision
12673  && (node->data_type->info.data_type.dec_precision != DB_DEFAULT_SCALE
12674  || node->data_type->info.data_type.dec_precision != DB_DEFAULT_NUMERIC_SCALE))
12675  {
12676  precision++; /* for decimal point */
12677  }
12678  break;
12679  case PT_TYPE_CHAR:
12680  if (node->data_type != NULL)
12681  {
12682  precision = node->data_type->info.data_type.precision;
12683  }
12684 
12686  {
12688  }
12689  break;
12690  case PT_TYPE_VARCHAR:
12691  if (node->data_type != NULL)
12692  {
12693  precision = node->data_type->info.data_type.precision;
12694  }
12695 
12697  {
12699  }
12700  break;
12701  case PT_TYPE_NCHAR:
12702  if (node->data_type != NULL)
12703  {
12704  precision = node->data_type->info.data_type.precision;
12705  }
12706 
12708  {
12710  }
12711  break;
12712  case PT_TYPE_VARNCHAR:
12713  if (node->data_type != NULL)
12714  {
12715  precision = node->data_type->info.data_type.precision;
12716  }
12717 
12719  {
12721  }
12722  break;
12723  case PT_TYPE_NULL:
12724  case PT_TYPE_NA:
12725  precision = 0;
12726  break;
12727 
12728  default:
12729  /* for host vars */
12730  switch (coerce_type)
12731  {
12732  case PT_TYPE_VARCHAR:
12734  break;
12735  case PT_TYPE_VARNCHAR:
12737  break;
12738  default:
12740  break;
12741  }
12742  break;
12743  }
12744 
12745  return precision;
12746 }
12747 
12748 /*
12749  * pt_eval_function_type () -
12750  * return: returns a node of the same type.
12751  * parser(in): parser global context info for reentrancy
12752  * node(in): a parse tree node of type PT_FUNCTION denoting an
12753  * an expression with aggregate functions.
12754  */
12755 
12756 static PT_NODE *
12757 pt_eval_function_type (PARSER_CONTEXT * parser, PT_NODE * node)
12758 {
12759  PT_NODE *arg_list;
12760  PT_TYPE_ENUM arg_type;
12761  FUNC_TYPE fcode;
12762  bool check_agg_single_arg = false;
12763  bool is_agg_function = false;
12764  PT_NODE *prev = NULL;
12765  PT_NODE *arg = NULL;
12766 
12767  is_agg_function = pt_is_aggregate_function (parser, node);
12768  arg_list = node->info.function.arg_list;
12769  fcode = node->info.function.function_type;
12770 
12771  if (!arg_list && fcode != PT_COUNT_STAR && fcode != PT_GROUPBY_NUM && fcode != PT_ROW_NUMBER && fcode != PT_RANK
12772  && fcode != PT_DENSE_RANK && fcode != PT_CUME_DIST && fcode != PT_PERCENT_RANK)
12773  {
12776  return node;
12777  }
12778 
12779  /* to avoid "node->next" ambiguities, wrap any logical node within the arg list with a cast to integer. This way, the
12780  * CNF trees do not mix up with the arg list. */
12781  for (arg = arg_list; arg != NULL; prev = arg, arg = arg->next)
12782  {
12783  if (arg->type_enum == PT_TYPE_LOGICAL)
12784  {
12786  if (arg == NULL)
12787  {
12788  /* the error message is set by pt_wrap_with_cast_op */
12789  node->type_enum = PT_TYPE_NONE;
12790  return node;
12791  }
12792  if (prev != NULL)
12793  {
12794  prev->next = arg;
12795  }
12796  else
12797  {
12798  node->info.function.arg_list = arg_list = arg;
12799  }
12800  }
12801  }
12802 
12803  if (pt_list_has_logical_nodes (arg_list))
12804  {
12806  pt_show_function (fcode), "boolean");
12807  return node;
12808  }
12809 
12810  /*
12811  * Should only get one arg to function; set to 0 if the function
12812  * accepts more than one.
12813  */
12814  check_agg_single_arg = true;
12815  arg_type = (arg_list) ? arg_list->type_enum : PT_TYPE_NONE;
12816 
12817  switch (fcode)
12818  {
12819  case PT_STDDEV:
12820  case PT_STDDEV_POP:
12821  case PT_STDDEV_SAMP:
12822  case PT_VARIANCE:
12823  case PT_VAR_POP:
12824  case PT_VAR_SAMP:
12825  case PT_AVG:
12826  if (arg_type != PT_TYPE_MAYBE && arg_type != PT_TYPE_NULL && arg_type != PT_TYPE_NA)
12827  {
12828  if (!PT_IS_NUMERIC_TYPE (arg_type))
12829  {
12831  if (arg_list == NULL)
12832  {
12833  return node;
12834  }
12835  }
12836  node->info.function.arg_list = arg_list;
12837  }
12838 
12839  arg_type = PT_TYPE_DOUBLE;
12840  break;
12841 
12842  case PT_AGG_BIT_AND:
12843  case PT_AGG_BIT_OR:
12844  case PT_AGG_BIT_XOR:
12845  if (!PT_IS_DISCRETE_NUMBER_TYPE (arg_type) && arg_type != PT_TYPE_MAYBE && arg_type != PT_TYPE_NULL
12846  && arg_type != PT_TYPE_NA)
12847  {
12848  /* cast arg_list to bigint */
12849  arg_list = pt_wrap_with_cast_op (parser, arg_list, PT_TYPE_BIGINT, 0, 0, NULL);
12850  if (arg_list == NULL)
12851  {
12852  return node;
12853  }
12854  arg_type = PT_TYPE_BIGINT;
12855  node->info.function.arg_list = arg_list;
12856  }
12857  break;
12858 
12859  case PT_SUM:
12860  if (!PT_IS_NUMERIC_TYPE (arg_type) && arg_type != PT_TYPE_MAYBE && arg_type != PT_TYPE_NULL
12861  && arg_type != PT_TYPE_NA && !pt_is_set_type (arg_list))
12862  {
12863  /* To display the sum as integer and not scientific */
12864  PT_TYPE_ENUM cast_type = (arg_type == PT_TYPE_ENUMERATION ? PT_TYPE_INTEGER : PT_TYPE_DOUBLE);
12865 
12866  /* cast arg_list to double or integer */
12867  arg_list = pt_wrap_with_cast_op (parser, arg_list, cast_type, 0, 0, NULL);
12868  if (arg_list == NULL)
12869  {
12870  return node;
12871  }
12872  arg_type = cast_type;
12873  node->info.function.arg_list = arg_list;
12874  }
12875  break;
12876 
12877  case PT_MAX:
12878  case PT_MIN:
12879  case PT_FIRST_VALUE:
12880  case PT_LAST_VALUE:
12881  case PT_NTH_VALUE:
12882  if (!PT_IS_NUMERIC_TYPE (arg_type) && !PT_IS_STRING_TYPE (arg_type) && !PT_IS_DATE_TIME_TYPE (arg_type)
12883  && arg_type != PT_TYPE_ENUMERATION && arg_type != PT_TYPE_MAYBE && arg_type != PT_TYPE_NULL
12884  && arg_type != PT_TYPE_NA)
12885  {
12887  pt_show_function (fcode), pt_show_type_enum (arg_type));
12888  }
12889  break;
12890 
12891  case PT_LEAD:
12892  case PT_LAG:
12893  case PT_COUNT:
12894  break;
12895 
12896  case PT_GROUP_CONCAT:
12897  {
12898  PT_TYPE_ENUM sep_type;
12899  sep_type = (arg_list->next) ? arg_list->next->type_enum : PT_TYPE_NONE;
12900  check_agg_single_arg = false;
12901 
12902  if (!PT_IS_NUMERIC_TYPE (arg_type) && !PT_IS_STRING_TYPE (arg_type) && !PT_IS_DATE_TIME_TYPE (arg_type)
12903  && arg_type != PT_TYPE_ENUMERATION && arg_type != PT_TYPE_MAYBE && arg_type != PT_TYPE_NULL
12904  && arg_type != PT_TYPE_NA)
12905  {
12907  pt_show_function (fcode), pt_show_type_enum (arg_type));
12908  break;
12909  }
12910 
12911  if (!PT_IS_STRING_TYPE (sep_type) && sep_type != PT_TYPE_NONE)
12912  {
12914  pt_show_function (fcode), pt_show_type_enum (sep_type));
12915  break;
12916  }
12917 
12918  if ((sep_type == PT_TYPE_NCHAR || sep_type == PT_TYPE_VARNCHAR) && arg_type != PT_TYPE_NCHAR
12919  && arg_type != PT_TYPE_VARNCHAR)
12920  {
12922  pt_show_function (fcode), pt_show_type_enum (arg_type), pt_show_type_enum (sep_type));
12923  break;
12924  }
12925 
12926  if ((arg_type == PT_TYPE_NCHAR || arg_type == PT_TYPE_VARNCHAR) && sep_type != PT_TYPE_NCHAR
12927  && sep_type != PT_TYPE_VARNCHAR && sep_type != PT_TYPE_NONE)
12928  {
12930  pt_show_function (fcode), pt_show_type_enum (arg_type), pt_show_type_enum (sep_type));
12931  break;
12932  }
12933 
12934  if ((arg_type == PT_TYPE_BIT || arg_type == PT_TYPE_VARBIT) && sep_type != PT_TYPE_BIT
12935  && sep_type != PT_TYPE_VARBIT && sep_type != PT_TYPE_NONE)
12936  {
12938  pt_show_function (fcode), pt_show_type_enum (arg_type), pt_show_type_enum (sep_type));
12939  break;
12940  }
12941 
12942  if ((arg_type != PT_TYPE_BIT && arg_type != PT_TYPE_VARBIT)
12943  && (sep_type == PT_TYPE_BIT || sep_type == PT_TYPE_VARBIT))
12944  {
12946  pt_show_function (fcode), pt_show_type_enum (arg_type), pt_show_type_enum (sep_type));
12947  break;
12948  }
12949  }
12950  break;
12951 
12952  case PT_CUME_DIST:
12953  case PT_PERCENT_RANK:
12954  check_agg_single_arg = false;
12955  break;
12956 
12957  case PT_NTILE:
12958  if (!PT_IS_DISCRETE_NUMBER_TYPE (arg_type) && arg_type != PT_TYPE_MAYBE && arg_type != PT_TYPE_NULL
12959  && arg_type != PT_TYPE_NA)
12960  {
12961  /* cast arg_list to double */
12962  arg_list = pt_wrap_with_cast_op (parser, arg_list, PT_TYPE_DOUBLE, 0, 0, NULL);
12963  if (arg_list == NULL)
12964  {
12965  return node;
12966  }
12967 
12968  arg_type = PT_TYPE_INTEGER;
12969  node->info.function.arg_list = arg_list;
12970  }
12971  break;
12972 
12973  case F_JSON_OBJECT:
12974  {
12975  PT_TYPE_ENUM unsupported_type;
12976  bool is_supported = false;
12977 
12978  PT_NODE *arg = arg_list;
12979  unsigned int index = 0;
12980 
12981  while (arg)
12982  {
12983  if (index % 2 == 0)
12984  {
12985  is_supported = pt_is_json_object_name (arg->type_enum);
12986  }
12987  else
12988  {
12989  is_supported = pt_is_json_value_type (arg->type_enum);
12990  }
12991 
12992  if (!is_supported)
12993  {
12994  unsupported_type = arg->type_enum;
12995  break;
12996  }
12997 
12998  arg = arg->next;
12999  index++;
13000  }
13001 
13002  if (!is_supported)
13003  {
13004  arg_type = PT_TYPE_NONE;
13006  pt_show_function (fcode), pt_show_type_enum (unsupported_type));
13007  }
13008  else
13009  {
13010  arg_type = PT_TYPE_JSON;
13011  }
13012  }
13013  break;
13014 
13015  case F_JSON_ARRAY:
13016  {
13017  PT_TYPE_ENUM unsupported_type;
13018  bool is_supported = false;
13019 
13020  PT_NODE *arg = arg_list;
13021 
13022  while (arg)
13023  {
13024  is_supported = pt_is_json_value_type (arg->type_enum);
13025 
13026  if (!is_supported)
13027  {
13028  unsupported_type = arg->type_enum;
13029  break;
13030  }
13031  arg = arg->next;
13032  }
13033 
13034  if (!is_supported)
13035  {
13036  arg_type = PT_TYPE_NONE;
13038  pt_show_function (fcode), pt_show_type_enum (unsupported_type));
13039  }
13040  else
13041  {
13042  arg_type = PT_TYPE_JSON;
13043  }
13044  }
13045  break;
13046 
13047  case F_JSON_MERGE:
13048  {
13049  PT_TYPE_ENUM unsupported_type;
13050  bool is_supported;
13051 
13052  PT_NODE *arg = arg_list;
13053 
13054  while (arg)
13055  {
13056  is_supported = pt_is_json_doc_type (arg->type_enum);
13057 
13058  if (!is_supported)
13059  {
13060  unsupported_type = arg->type_enum;
13061  break;
13062  }
13063  arg = arg->next;
13064  }
13065 
13066  if (!is_supported)
13067  {
13068  arg_type = PT_TYPE_NONE;
13070  pt_show_function (fcode), pt_show_type_enum (unsupported_type));
13071  }
13072  else
13073  {
13074  arg_type = PT_TYPE_JSON;
13075  }
13076  }
13077  break;
13078 
13079  case F_JSON_INSERT:
13080  case F_JSON_REPLACE:
13081  case F_JSON_SET:
13082  case F_JSON_ARRAY_APPEND:
13083  {
13084  PT_TYPE_ENUM unsupported_type;
13085  unsigned int index = 0;
13086  bool is_supported = false;
13087  PT_NODE *arg = arg_list;
13088 
13089  is_supported = pt_is_json_doc_type (arg->type_enum);
13090  if (!is_supported)
13091  {
13092  arg_type = PT_TYPE_NONE;
13094  pt_show_function (fcode), pt_show_type_enum (arg->type_enum));
13095  break;
13096  }
13097 
13098  arg = arg->next;
13099  while (arg)
13100  {
13101  if (index % 2 == 0)
13102  {
13103  is_supported = pt_is_json_path (arg->type_enum);
13104  }
13105  else
13106  {
13107  is_supported = pt_is_json_doc_type (arg->type_enum);
13108  }
13109 
13110  if (!is_supported)
13111  {
13112  unsupported_type = arg->type_enum;
13113  break;
13114  }
13115 
13116  arg = arg->next;
13117  index++;
13118  }
13119  if (!is_supported)
13120  {
13121  arg_type = PT_TYPE_NONE;
13123  pt_show_function (fcode), pt_show_type_enum (unsupported_type));
13124  }
13125  }
13126  break;
13127 
13128  case F_JSON_REMOVE:
13129  {
13130  PT_TYPE_ENUM unsupported_type;
13131  bool is_supported = false;
13132  PT_NODE *arg = arg_list;
13133 
13134  is_supported = pt_is_json_doc_type (arg->type_enum);
13135 
13136  if (!is_supported)
13137  {
13138  arg_type = PT_TYPE_NONE;
13140  pt_show_function (fcode), pt_show_type_enum (arg->type_enum));
13141  break;
13142  }
13143 
13144  arg = arg->next;
13145  while (arg)
13146  {
13147  is_supported = pt_is_json_path (arg->type_enum);
13148 
13149  if (!is_supported)
13150  {
13151  unsupported_type = arg->type_enum;
13152  break;
13153  }
13154 
13155  arg = arg->next;
13156  }
13157 
13158  if (!is_supported)
13159  {
13160  arg_type = PT_TYPE_NONE;
13162  pt_show_function (fcode), pt_show_type_enum (unsupported_type));
13163  }
13164  }
13165  break;
13166 
13167  case F_JSON_GET_ALL_PATHS:
13168  {
13169  PT_NODE *arg = arg_list;
13170  bool is_supported = false;
13171 
13172  is_supported = pt_is_json_doc_type (arg->type_enum);
13173  if (!is_supported)
13174  {
13175  arg_type = PT_TYPE_NONE;
13177  pt_show_function (fcode), pt_show_type_enum (arg->type_enum));
13178  }
13179  }
13180  break;
13181 
13182  case F_JSON_KEYS:
13183  {
13184  // should have maximum 2 parameters
13185  PT_NODE *arg = arg_list;
13186  PT_TYPE_ENUM unsupported_type;
13187  unsigned int index = 0;
13188  bool is_supported = false;
13189 
13190  while (arg)
13191  {
13192  switch (index)
13193  {
13194  case 0:
13195  is_supported = pt_is_json_doc_type (arg->type_enum);
13196  break;
13197  case 1:
13198  is_supported = pt_is_json_path (arg->type_enum);;
13199  break;
13200  default:
13201  /* Should not happen */
13202  assert (false);
13203  break;
13204  }
13205 
13206  if (!is_supported)
13207  {
13208  unsupported_type = arg->type_enum;
13209  arg_type = PT_TYPE_NONE;
13211  pt_show_function (fcode), pt_show_type_enum (unsupported_type));
13212  break;
13213  }
13214 
13215  index++;
13216  arg = arg->next;
13217  }
13218  }
13219  break;
13220 
13221  case F_ELT:
13222  {
13223  /* all types used in the arguments list */
13224  bool has_arg_type[PT_TYPE_MAX - PT_TYPE_MIN] = { false };
13225 
13226  /* a subset of argument types given to ELT that can not be cast to [N]CHAR VARYING */
13227  PT_TYPE_ENUM bad_types[4] = {
13229  };
13230 
13231  PT_NODE *arg = arg_list;
13232 
13233  size_t i = 0; /* used to index has_arg_type */
13234  size_t num_bad = 0; /* used to index bad_types */
13235 
13236  memset (has_arg_type, 0, sizeof (has_arg_type));
13237 
13238  /* check the index argument (null, numeric or host var) */
13239  if (PT_IS_NUMERIC_TYPE (arg->type_enum) || PT_IS_CHAR_STRING_TYPE (arg->type_enum)
13240  || arg->type_enum == PT_TYPE_NONE || arg->type_enum == PT_TYPE_NA || arg->type_enum == PT_TYPE_NULL
13241  || arg->type_enum == PT_TYPE_MAYBE)
13242  {
13243  arg = arg->next;
13244  }
13245  else
13246  {
13247  arg_type = PT_TYPE_NONE;
13249  pt_show_function (fcode), pt_show_type_enum (arg->type_enum));
13250  break;
13251  }
13252 
13253  /* make a list of all other argument types (null, [N]CHAR [VARYING], or host var) */
13254  while (arg)
13255  {
13256  if (arg->type_enum < PT_TYPE_MAX)
13257  {
13258  has_arg_type[arg->type_enum - PT_TYPE_MIN] = true;
13259  arg = arg->next;
13260  }
13261  else
13262  {
13263  assert (false); /* invalid data type */
13264  arg_type = PT_TYPE_NONE;
13266  pt_show_function (fcode), pt_show_type_enum (arg->type_enum));
13267  break;
13268  }
13269  }
13270 
13271  /* look for unsupported argument types in the list */
13272  while (i < (sizeof (has_arg_type) / sizeof (has_arg_type[0])))
13273  {
13274  if (has_arg_type[i])
13275  {
13279  && (PT_TYPE_MIN + i != PT_TYPE_NA) && (PT_TYPE_MIN + i != PT_TYPE_NULL)
13280  && (PT_TYPE_MIN + i != PT_TYPE_MAYBE))
13281  {
13282  /* type is not NULL, unknown and is not known coercible to [N]CHAR VARYING */
13283  size_t k = 0;
13284 
13285  while (k < num_bad && bad_types[k] != PT_TYPE_MIN + i)
13286  {
13287  k++;
13288  }
13289 
13290  if (k == num_bad)
13291  {
13292  bad_types[num_bad++] = (PT_TYPE_ENUM) (PT_TYPE_MIN + i);
13293 
13294  if (num_bad == sizeof (bad_types) / sizeof (bad_types[0]))
13295  {
13296  break;
13297  }
13298  }
13299  }
13300  }
13301 
13302  i++;
13303  }
13304 
13305  /* check string category (CHAR or NCHAR) for any string arguments */
13306  if ((num_bad < sizeof (bad_types) / sizeof (bad_types[0]) - 1)
13307  && (has_arg_type[PT_TYPE_CHAR - PT_TYPE_MIN] || has_arg_type[PT_TYPE_VARCHAR - PT_TYPE_MIN])
13308  && (has_arg_type[PT_TYPE_NCHAR - PT_TYPE_MIN] || has_arg_type[PT_TYPE_VARNCHAR - PT_TYPE_MIN]))
13309  {
13310  if (has_arg_type[PT_TYPE_CHAR - PT_TYPE_MIN])
13311  {
13312  bad_types[num_bad++] = PT_TYPE_CHAR;
13313  }
13314  else
13315  {
13316  bad_types[num_bad++] = PT_TYPE_VARCHAR;
13317  }
13318 
13319  if (has_arg_type[PT_TYPE_NCHAR - PT_TYPE_MIN])
13320  {
13321  bad_types[num_bad++] = PT_TYPE_NCHAR;
13322  }
13323  else
13324  {
13325  bad_types[num_bad++] = PT_TYPE_VARNCHAR;
13326  }
13327  }
13328 
13329  /* report any unsupported arguments */
13330  switch (num_bad)
13331  {
13332  case 1:
13333  arg_type = PT_TYPE_NONE;
13335  pt_show_function (fcode), pt_show_type_enum (bad_types[0]));
13336  break;
13337  case 2:
13338  arg_type = PT_TYPE_NONE;
13340  pt_show_function (fcode), pt_show_type_enum (bad_types[0]), pt_show_type_enum (bad_types[1]));
13341  break;
13342  case 3:
13343  arg_type = PT_TYPE_NONE;
13345  pt_show_function (fcode), pt_show_type_enum (bad_types[0]), pt_show_type_enum (bad_types[1]),
13346  pt_show_type_enum (bad_types[2]));
13347  break;
13348  case 4:
13349  arg_type = PT_TYPE_NONE;
13351  pt_show_function (fcode), pt_show_type_enum (bad_types[0]), pt_show_type_enum (bad_types[1]),
13352  pt_show_type_enum (bad_types[2]), pt_show_type_enum (bad_types[3]));
13353  break;
13354  }
13355  }
13356  break;
13357 
13358  case F_INSERT_SUBSTRING:
13359  {
13360  PT_TYPE_ENUM arg1_type = PT_TYPE_NONE, arg2_type = PT_TYPE_NONE, arg3_type = PT_TYPE_NONE, arg4_type =
13361  PT_TYPE_NONE;
13363  int num_args = 0;
13364  /* arg_list to array */
13365  if (pt_node_list_to_array (parser, arg_list, arg_array, NUM_F_INSERT_SUBSTRING_ARGS, &num_args) != NO_ERROR)
13366  {
13367  break;
13368  }
13369  if (num_args != NUM_F_INSERT_SUBSTRING_ARGS)
13370  {
13371  assert (false);
13372  break;
13373  }
13374 
13375  arg1_type = arg_array[0]->type_enum;
13376  arg2_type = arg_array[1]->type_enum;
13377  arg3_type = arg_array[2]->type_enum;
13378  arg4_type = arg_array[3]->type_enum;
13379  /* check arg2 and arg3 */
13380  if (!PT_IS_NUMERIC_TYPE (arg2_type) && !PT_IS_CHAR_STRING_TYPE (arg2_type) && arg2_type != PT_TYPE_MAYBE
13381  && arg2_type != PT_TYPE_NULL && arg2_type != PT_TYPE_NA)
13382  {
13383  arg_type = PT_TYPE_NONE;
13385  pt_show_function (fcode), pt_show_type_enum (arg1_type), pt_show_type_enum (arg2_type),
13386  pt_show_type_enum (arg3_type), pt_show_type_enum (arg4_type));
13387  break;
13388  }
13389 
13390  if (!PT_IS_NUMERIC_TYPE (arg3_type) && !PT_IS_CHAR_STRING_TYPE (arg3_type) && arg3_type != PT_TYPE_MAYBE
13391  && arg3_type != PT_TYPE_NULL && arg3_type != PT_TYPE_NA)
13392  {
13393  arg_type = PT_TYPE_NONE;
13395  pt_show_function (fcode), pt_show_type_enum (arg1_type), pt_show_type_enum (arg2_type),
13396  pt_show_type_enum (arg3_type), pt_show_type_enum (arg4_type));
13397  break;
13398  }
13399 
13400  /* check arg1 */
13401  if (!PT_IS_NUMERIC_TYPE (arg1_type) && !PT_IS_STRING_TYPE (arg1_type) && !PT_IS_DATE_TIME_TYPE (arg1_type)
13402  && arg1_type != PT_TYPE_ENUMERATION && arg1_type != PT_TYPE_MAYBE && arg1_type != PT_TYPE_NULL
13403  && arg1_type != PT_TYPE_NA)
13404  {
13405  arg_type = PT_TYPE_NONE;
13407  pt_show_function (fcode), pt_show_type_enum (arg1_type), pt_show_type_enum (arg2_type),
13408  pt_show_type_enum (arg3_type), pt_show_type_enum (arg4_type));
13409  break;
13410  }
13411  /* check arg4 */
13412  if (!PT_IS_NUMERIC_TYPE (arg4_type) && !PT_IS_STRING_TYPE (arg4_type) && !PT_IS_DATE_TIME_TYPE (arg4_type)
13413  && arg4_type != PT_TYPE_MAYBE && arg4_type != PT_TYPE_NULL && arg4_type != PT_TYPE_NA)
13414  {
13415  arg_type = PT_TYPE_NONE;
13417  pt_show_function (fcode), pt_show_type_enum (arg1_type), pt_show_type_enum (arg2_type),
13418  pt_show_type_enum (arg3_type), pt_show_type_enum (arg4_type));
13419  break;
13420  }
13421  }
13422  break;
13423 
13424  case PT_MEDIAN:
13425  case PT_PERCENTILE_CONT:
13426  case PT_PERCENTILE_DISC:
13427  if (arg_type != PT_TYPE_NULL && arg_type != PT_TYPE_NA && !PT_IS_NUMERIC_TYPE (arg_type)
13428  && !PT_IS_STRING_TYPE (arg_type) && !PT_IS_DATE_TIME_TYPE (arg_type) && arg_type != PT_TYPE_MAYBE)
13429  {
13431  pt_show_function (fcode), pt_show_type_enum (arg_type));
13432  }
13433 
13434  break;
13435 
13436  default:
13437  check_agg_single_arg = false;
13438  break;
13439  }
13440 
13441  if (is_agg_function && check_agg_single_arg)
13442  {
13443  if (arg_list->next)
13444  {
13447  }
13448  else
13449  {
13450  /* do special constant folding; COUNT(1), COUNT(?), COUNT(:x), ... -> COUNT(*) */
13451  /* TODO does this belong to type checking or constant folding? */
13452  if (pt_is_const (arg_list))
13453  {
13454  PT_MISC_TYPE all_or_distinct;
13455 
13456  all_or_distinct = node->info.function.all_or_distinct;
13457  if (fcode == PT_COUNT && all_or_distinct != PT_DISTINCT)
13458  {
13459  fcode = node->info.function.function_type = PT_COUNT_STAR;
13460  parser_free_tree (parser, arg_list);
13461  arg_list = node->info.function.arg_list = NULL;
13462  }
13463  }
13464  }
13465  }
13466 
13467  if (node->type_enum == PT_TYPE_NONE || node->data_type == NULL)
13468  {
13469  /* determine function result type */
13470  switch (fcode)
13471  {
13472  case PT_COUNT:
13473  case PT_COUNT_STAR:
13474  case PT_ROW_NUMBER:
13475  case PT_RANK:
13476  case PT_DENSE_RANK:
13477  case PT_NTILE:
13478  node->type_enum = PT_TYPE_INTEGER;
13479  break;
13480 
13481  case PT_CUME_DIST:
13482  case PT_PERCENT_RANK:
13483  node->type_enum = PT_TYPE_DOUBLE;
13484  break;
13485 
13486  case PT_GROUPBY_NUM:
13487  node->type_enum = PT_TYPE_BIGINT;
13488  break;
13489 
13490  case PT_AGG_BIT_AND:
13491  case PT_AGG_BIT_OR:
13492  case PT_AGG_BIT_XOR:
13493  node->type_enum = PT_TYPE_BIGINT;
13494  break;
13495 
13496  case F_TABLE_SET:
13497  node->type_enum = PT_TYPE_SET;
13498  pt_add_type_to_set (parser, pt_get_select_list (parser, arg_list), &node->data_type);
13499  break;
13500 
13501  case F_TABLE_MULTISET:
13502  node->type_enum = PT_TYPE_MULTISET;
13503  pt_add_type_to_set (parser, pt_get_select_list (parser, arg_list), &node->data_type);
13504  break;
13505 
13506  case F_TABLE_SEQUENCE:
13507  node->type_enum = PT_TYPE_SEQUENCE;
13508  pt_add_type_to_set (parser, pt_get_select_list (parser, arg_list), &node->data_type);
13509  break;
13510 
13511  case F_SET:
13512  node->type_enum = PT_TYPE_SET;
13513  pt_add_type_to_set (parser, arg_list, &node->data_type);
13514  break;
13515 
13516  case F_MULTISET:
13517  node->type_enum = PT_TYPE_MULTISET;
13518  pt_add_type_to_set (parser, arg_list, &node->data_type);
13519  break;
13520 
13521  case F_SEQUENCE:
13522  node->type_enum = PT_TYPE_SEQUENCE;
13523  pt_add_type_to_set (parser, arg_list, &node->data_type);
13524  break;
13525 
13526  case PT_SUM:
13527  node->type_enum = arg_type;
13528  node->data_type = parser_copy_tree_list (parser, arg_list->data_type);
13529  if (arg_type == PT_TYPE_NUMERIC && node->data_type)
13530  {
13531  node->data_type->info.data_type.precision = DB_MAX_NUMERIC_PRECISION;
13532  }
13533  break;
13534 
13535  case PT_AVG:
13536  case PT_STDDEV:
13537  case PT_STDDEV_POP:
13538  case PT_STDDEV_SAMP:
13539  case PT_VARIANCE:
13540  case PT_VAR_POP:
13541  case PT_VAR_SAMP:
13542  node->type_enum = arg_type;
13543  node->data_type = NULL;
13544 
13545  break;
13546  case F_JSON_OBJECT:
13547  case F_JSON_ARRAY:
13548  case F_JSON_INSERT:
13549  case F_JSON_REPLACE:
13550  case F_JSON_SET:
13551  case F_JSON_KEYS:
13552  case F_JSON_REMOVE:
13553  case F_JSON_ARRAY_APPEND:
13554  case F_JSON_MERGE:
13555  case F_JSON_GET_ALL_PATHS:
13556  node->type_enum = PT_TYPE_JSON;
13557  break;
13558  case PT_MEDIAN:
13559  case PT_PERCENTILE_CONT:
13560  case PT_PERCENTILE_DISC:
13561  /* let calculation decide the type */
13562  node->type_enum = PT_TYPE_MAYBE;
13563  node->data_type = NULL;
13564 
13565  break;
13566 
13567  case PT_GROUP_CONCAT:
13568  {
13569  if (arg_type == PT_TYPE_NCHAR || arg_type == PT_TYPE_VARNCHAR)
13570  {
13571  node->type_enum = PT_TYPE_VARNCHAR;
13573  if (node->data_type == NULL)
13574  {
13575  assert (false);
13576  }
13577  node->data_type->info.data_type.precision = TP_FLOATING_PRECISION_VALUE;
13578  }
13579  else if (arg_type == PT_TYPE_BIT || arg_type == PT_TYPE_VARBIT)
13580  {
13581  node->type_enum = PT_TYPE_VARBIT;
13583  if (node->data_type == NULL)
13584  {
13585  node->type_enum = PT_TYPE_NONE;
13586  assert (false);
13587  }
13588  node->data_type->info.data_type.precision = TP_FLOATING_PRECISION_VALUE;
13589  }
13590  else
13591  {
13592  node->type_enum = PT_TYPE_VARCHAR;
13594  if (node->data_type == NULL)
13595  {
13596  node->type_enum = PT_TYPE_NONE;
13597  assert (false);
13598  }
13599  node->data_type->info.data_type.precision = TP_FLOATING_PRECISION_VALUE;
13600  }
13601  }
13602  break;
13603 
13604  case F_INSERT_SUBSTRING:
13605  {
13606  PT_NODE *new_att = NULL;
13607  PT_TYPE_ENUM arg1_type = PT_TYPE_NONE, arg2_type = PT_TYPE_NONE, arg3_type = PT_TYPE_NONE, arg4_type =
13608  PT_TYPE_NONE;
13609  PT_TYPE_ENUM arg1_orig_type, arg2_orig_type, arg3_orig_type, arg4_orig_type;
13611  int num_args;
13612 
13613  /* arg_list to array */
13614  if (pt_node_list_to_array (parser, arg_list, arg_array, NUM_F_INSERT_SUBSTRING_ARGS, &num_args) != NO_ERROR)
13615  {
13616  break;
13617  }
13618  if (num_args != NUM_F_INSERT_SUBSTRING_ARGS)
13619  {
13620  assert (false);
13621  break;
13622  }
13623 
13624  arg1_orig_type = arg1_type = arg_array[0]->type_enum;
13625  arg2_orig_type = arg2_type = arg_array[1]->type_enum;
13626  arg3_orig_type = arg3_type = arg_array[2]->type_enum;
13627  arg4_orig_type = arg4_type = arg_array[3]->type_enum;
13628  arg_type = PT_TYPE_NONE;
13629 
13630  /* validate and/or convert arguments */
13631  /* arg1 should be VAR-str, but compatible with arg4 (except when arg4 is BIT - no casting to bit on arg1) */
13632  if (!(PT_IS_STRING_TYPE (arg1_type)))
13633  {
13634  PT_TYPE_ENUM upgraded_type = PT_TYPE_NONE;
13635  if (arg4_type == PT_TYPE_NCHAR)
13636  {
13637  upgraded_type = PT_TYPE_VARNCHAR;
13638  }
13639  else
13640  {
13641  upgraded_type = PT_TYPE_VARCHAR;
13642  }
13643 
13644  new_att =
13645  pt_wrap_with_cast_op (parser, arg_array[0], upgraded_type, TP_FLOATING_PRECISION_VALUE, 0, NULL);
13646  if (new_att == NULL)
13647  {
13648  break;
13649  }
13650  node->info.function.arg_list = arg_array[0] = new_att;
13651  arg_type = arg1_type = upgraded_type;
13652  }
13653  else
13654  {
13655  arg_type = arg1_type;
13656  }
13657 
13658 
13659  if (arg2_type != PT_TYPE_INTEGER)
13660  {
13661  new_att =
13663  if (new_att == NULL)
13664  {
13665  break;
13666  }
13667  arg_array[0]->next = arg_array[1] = new_att;
13668  arg2_type = PT_TYPE_INTEGER;
13669 
13670  }
13671 
13672  if (arg3_type != PT_TYPE_INTEGER)
13673  {
13674  new_att =
13676  if (new_att == NULL)
13677  {
13678  break;
13679  }
13680  arg_array[1]->next = arg_array[2] = new_att;
13681  arg3_type = PT_TYPE_INTEGER;
13682  }
13683 
13684  /* set result type and precision */
13685  if (arg_type == PT_TYPE_NCHAR || arg_type == PT_TYPE_VARNCHAR)
13686  {
13687  node->type_enum = PT_TYPE_VARNCHAR;
13689  node->data_type->info.data_type.precision = TP_FLOATING_PRECISION_VALUE;
13690  }
13691  else if (arg_type == PT_TYPE_BIT || arg_type == PT_TYPE_VARBIT)
13692  {
13693  node->type_enum = PT_TYPE_VARBIT;
13695  node->data_type->info.data_type.precision = TP_FLOATING_PRECISION_VALUE;
13696  }
13697  else
13698  {
13699  arg_type = node->type_enum = PT_TYPE_VARCHAR;
13701  node->data_type->info.data_type.precision = TP_FLOATING_PRECISION_VALUE;
13702  }
13703  /* validate and/or set arg4 */
13704  if (!(PT_IS_STRING_TYPE (arg4_type)))
13705  {
13706  new_att = pt_wrap_with_cast_op (parser, arg_array[3], arg_type, TP_FLOATING_PRECISION_VALUE, 0, NULL);
13707  if (new_att == NULL)
13708  {
13709  break;
13710  }
13711  arg_array[2]->next = arg_array[3] = new_att;
13712  }
13713  /* final check of arg and arg4 type matching */
13714  if ((arg4_type == PT_TYPE_VARNCHAR || arg4_type == PT_TYPE_NCHAR)
13715  && (arg_type == PT_TYPE_VARCHAR || arg_type == PT_TYPE_CHAR))
13716  {
13717  arg_type = PT_TYPE_NONE;
13719  pt_show_function (fcode), pt_show_type_enum (arg1_orig_type),
13720  pt_show_type_enum (arg2_orig_type), pt_show_type_enum (arg3_orig_type),
13721  pt_show_type_enum (arg4_orig_type));
13722  }
13723  else if ((arg_type == PT_TYPE_VARNCHAR || arg_type == PT_TYPE_NCHAR)
13724  && (arg4_type == PT_TYPE_VARCHAR || arg4_type == PT_TYPE_CHAR))
13725  {
13726  arg_type = PT_TYPE_NONE;
13728  pt_show_function (fcode), pt_show_type_enum (arg1_orig_type),
13729  pt_show_type_enum (arg2_orig_type), pt_show_type_enum (arg3_orig_type),
13730  pt_show_type_enum (arg4_orig_type));
13731  }
13732  else if ((arg_type == PT_TYPE_VARBIT || arg_type == PT_TYPE_BIT)
13733  && (arg4_type != PT_TYPE_VARBIT && arg4_type != PT_TYPE_BIT))
13734  {
13735  arg_type = PT_TYPE_NONE;
13737  pt_show_function (fcode), pt_show_type_enum (arg1_orig_type),
13738  pt_show_type_enum (arg2_orig_type), pt_show_type_enum (arg3_orig_type),
13739  pt_show_type_enum (arg4_orig_type));
13740  }
13741  else if ((arg4_type == PT_TYPE_VARBIT || arg4_type == PT_TYPE_BIT)
13742  && (arg_type != PT_TYPE_VARBIT && arg_type != PT_TYPE_BIT))
13743  {
13744  arg_type = PT_TYPE_NONE;
13746  pt_show_function (fcode), pt_show_type_enum (arg1_orig_type),
13747  pt_show_type_enum (arg2_orig_type), pt_show_type_enum (arg3_orig_type),
13748  pt_show_type_enum (arg4_orig_type));
13749  }
13750  }
13751  break;
13752 
13753  case F_ELT:
13754  {
13755  PT_NODE *new_att = NULL;
13756  PT_NODE *arg = arg_list, *prev_arg = arg_list;
13757  int max_precision = 0;
13758 
13759  /* check and cast the type for the index argument. */
13760  if (!PT_IS_DISCRETE_NUMBER_TYPE (arg->type_enum))
13761  {
13763 
13764  if (new_att)
13765  {
13766  prev_arg = arg_list = arg = new_att;
13767  node->info.function.arg_list = arg_list;
13768  }
13769  else
13770  {
13771  break;
13772  }
13773  }
13774 
13775  /*
13776  * Look for the first argument of character string type and obtain its category (CHAR/NCHAR). All other
13777  * arguments should be converted to this type, which is also the return type. */
13778 
13779  arg_type = PT_TYPE_NONE;
13780  arg = arg->next;
13781 
13782  while (arg && arg_type == PT_TYPE_NONE)
13783  {
13784  if (PT_IS_CHAR_STRING_TYPE (arg->type_enum))
13785  {
13786  if (arg->type_enum == PT_TYPE_CHAR || arg->type_enum == PT_TYPE_VARCHAR)
13787  {
13788  arg_type = PT_TYPE_VARCHAR;
13789  }
13790  else
13791  {
13792  arg_type = PT_TYPE_VARNCHAR;
13793  }
13794  }
13795  else
13796  {
13797  arg = arg->next;
13798  }
13799  }
13800 
13801  if (arg_type == PT_TYPE_NONE)
13802  {
13803  /* no [N]CHAR [VARYING] argument passed; convert them all to VARCHAR */
13804  arg_type = PT_TYPE_VARCHAR;
13805  }
13806 
13807  /* take the maximum precision among all value arguments */
13808  arg = arg_list->next;
13809 
13810  while (arg)
13811  {
13813 
13814  precision = pt_character_length_for_node (arg, arg_type);
13815  if (max_precision != TP_FLOATING_PRECISION_VALUE)
13816  {
13817  if (precision == TP_FLOATING_PRECISION_VALUE || max_precision < precision)
13818  {
13819  max_precision = precision;
13820  }
13821  }
13822 
13823  arg = arg->next;
13824  }
13825 
13826  /* cast all arguments to [N]CHAR VARYING(max_precision) */
13827  arg = arg_list->next;
13828  while (arg)
13829  {
13830  if (arg->type_enum != arg_type || arg->data_type->info.data_type.precision != max_precision)
13831  {
13832  PT_NODE *new_attr = pt_wrap_with_cast_op (parser, arg, arg_type,
13833  max_precision, 0, NULL);
13834 
13835  if (new_attr)
13836  {
13837  prev_arg->next = arg = new_attr;
13838  }
13839  else
13840  {
13841  break;
13842  }
13843  }
13844 
13845  arg = arg->next;
13846  prev_arg = prev_arg->next;
13847  }
13848 
13849  /* Return the selected data type and precision */
13850 
13851  node->data_type = pt_make_prim_data_type (parser, arg_type);
13852 
13853  if (node->data_type)
13854  {
13855  node->type_enum = arg_type;
13856  node->data_type->info.data_type.precision = max_precision;
13857  node->data_type->info.data_type.dec_precision = 0;
13858  }
13859  }
13860  break;
13861 
13862  default:
13863  /* otherwise, f(x) has same type as x */
13864  node->type_enum = arg_type;
13865  node->data_type = parser_copy_tree_list (parser, arg_list->data_type);
13866  break;
13867  }
13868  }
13869 
13870  /* collation checking */
13871  arg_list = node->info.function.arg_list;
13872  switch (fcode)
13873  {
13874  case PT_GROUP_CONCAT:
13875  {
13876  PT_COLL_INFER coll_infer1;
13877  PT_NODE *new_node;
13878  PT_TYPE_ENUM sep_type;
13879 
13880  (void) pt_get_collation_info (arg_list, &coll_infer1);
13881 
13882  sep_type = (arg_list->next) ? arg_list->next->type_enum : PT_TYPE_NONE;
13883  if (PT_HAS_COLLATION (sep_type))
13884  {
13885  assert (arg_list->next != NULL);
13886 
13887  new_node =
13888  pt_coerce_node_collation (parser, arg_list->next, coll_infer1.coll_id, coll_infer1.codeset, false, false,
13890 
13891  if (new_node == NULL)
13892  {
13893  goto error_collation;
13894  }
13895 
13896  arg_list->next = new_node;
13897  }
13898 
13899  if (arg_list->type_enum != PT_TYPE_MAYBE)
13900  {
13901  new_node =
13902  pt_coerce_node_collation (parser, node, coll_infer1.coll_id, coll_infer1.codeset, true, false,
13904 
13905  if (new_node == NULL)
13906  {
13907  goto error_collation;
13908  }
13909 
13910  node = new_node;
13911  }
13912  else if (node->data_type != NULL)
13913  {
13914  /* argument is not determined, collation of result will be resolved at execution */
13915  node->data_type->info.data_type.collation_flag = TP_DOMAIN_COLL_LEAVE;
13916  }
13917  }
13918  break;
13919 
13920  case F_INSERT_SUBSTRING:
13921  {
13922  PT_TYPE_ENUM arg1_type = PT_TYPE_NONE, arg4_type = PT_TYPE_NONE;
13924  int num_args = 0;
13925  PT_COLL_INFER coll_infer1, coll_infer4;
13926  INTL_CODESET common_cs = LANG_SYS_CODESET;
13927  int common_coll = LANG_SYS_COLLATION;
13928  PT_NODE *new_node;
13929  int args_w_coll = 0;
13930 
13931  coll_infer1.codeset = LANG_SYS_CODESET;
13932  coll_infer4.codeset = LANG_SYS_CODESET;
13933  coll_infer1.coll_id = LANG_SYS_COLLATION;
13934  coll_infer4.coll_id = LANG_SYS_COLLATION;
13937  coll_infer1.can_force_cs = true;
13938  coll_infer4.can_force_cs = true;
13939 
13940  if (pt_node_list_to_array (parser, arg_list, arg_array, NUM_F_INSERT_SUBSTRING_ARGS, &num_args) != NO_ERROR)
13941  {
13942  break;
13943  }
13944 
13945  if (num_args != NUM_F_INSERT_SUBSTRING_ARGS)
13946  {
13947  assert (false);
13948  break;
13949  }
13950 
13951  arg1_type = arg_array[0]->type_enum;
13952  arg4_type = arg_array[3]->type_enum;
13953 
13954  if (PT_HAS_COLLATION (arg1_type) || arg1_type == PT_TYPE_MAYBE)
13955  {
13956  if (pt_get_collation_info (arg_array[0], &coll_infer1))
13957  {
13958  args_w_coll++;
13959  }
13960 
13961  if (arg1_type != PT_TYPE_MAYBE)
13962  {
13963  common_coll = coll_infer1.coll_id;
13964  common_cs = coll_infer1.codeset;
13965  }
13966  }
13967 
13968  if (PT_HAS_COLLATION (arg4_type) || arg4_type == PT_TYPE_MAYBE)
13969  {
13970  if (pt_get_collation_info (arg_array[3], &coll_infer4))
13971  {
13972  args_w_coll++;
13973  }
13974 
13975  if (arg1_type != PT_TYPE_MAYBE)
13976  {
13977  common_coll = coll_infer4.coll_id;
13978  common_cs = coll_infer4.codeset;
13979  }
13980  }
13981 
13982  if (coll_infer1.coll_id == coll_infer4.coll_id)
13983  {
13984  assert (coll_infer1.codeset == coll_infer4.codeset);
13985  common_coll = coll_infer1.coll_id;
13986  common_cs = coll_infer1.codeset;
13987  }
13988  else
13989  {
13990  if (pt_common_collation (&coll_infer1, &coll_infer4, NULL, args_w_coll, false, &common_coll, &common_cs) !=
13991  0)
13992  {
13993  goto error_collation;
13994  }
13995  }
13996 
13997  /* coerce collation of arguments */
13998  if ((common_coll != coll_infer1.coll_id || common_cs != coll_infer1.codeset)
13999  && (PT_HAS_COLLATION (arg1_type) || arg1_type == PT_TYPE_MAYBE))
14000  {
14001  new_node =
14002  pt_coerce_node_collation (parser, arg_array[0], common_coll, common_cs, coll_infer1.can_force_cs, false,
14004  if (new_node == NULL)
14005  {
14006  goto error_collation;
14007  }
14008 
14009  node->info.function.arg_list = arg_array[0] = new_node;
14010  }
14011 
14012  /* coerce collation of arguments */
14013  if ((common_coll != coll_infer4.coll_id || common_cs != coll_infer4.codeset)
14014  && (PT_HAS_COLLATION (arg4_type) || arg4_type == PT_TYPE_MAYBE))
14015  {
14016  new_node =
14017  pt_coerce_node_collation (parser, arg_array[3], common_coll, common_cs, coll_infer4.can_force_cs, false,
14019  if (new_node == NULL)
14020  {
14021  goto error_collation;
14022  }
14023 
14024  arg_array[2]->next = arg_array[3] = new_node;
14025  }
14026 
14027  if ((arg_array[3]->type_enum == PT_TYPE_MAYBE || PT_IS_CAST_MAYBE (arg_array[3]))
14028  && (arg_array[0]->type_enum == PT_TYPE_MAYBE || PT_IS_CAST_MAYBE (arg_array[0])) && node->data_type != NULL)
14029  {
14030  node->data_type->info.data_type.collation_flag = TP_DOMAIN_COLL_LEAVE;
14031  }
14032  else
14033  {
14034  new_node =
14035  pt_coerce_node_collation (parser, node, common_coll, common_cs, true, false, PT_TYPE_NONE, PT_TYPE_NONE);
14036  if (new_node == NULL)
14037  {
14038  goto error_collation;
14039  }
14040 
14041  node = new_node;
14042  }
14043  }
14044  break;
14045 
14046  default:
14047  node = pt_check_function_collation (parser, node);
14048  break;
14049  }
14050 
14051  return node;
14052 
14053 error_collation:
14055 
14056  return node;
14057 }
14058 
14059 /*
14060  * pt_eval_method_call_type () -
14061  * return: returns a node of the same type.
14062  * parser(in): parser global context info for reentrancy
14063  * node(in): a parse tree node of type PT_METHOD_CALL.
14064  */
14065 
14066 static PT_NODE *
14067 pt_eval_method_call_type (PARSER_CONTEXT * parser, PT_NODE * node)
14068 {
14069  PT_NODE *method_name;
14070  PT_NODE *on_call_target;
14071  DB_OBJECT *obj = (DB_OBJECT *) 0;
14072  const char *name = (const char *) 0;
14073  DB_METHOD *method = (DB_METHOD *) 0;
14074  DB_DOMAIN *domain;
14075  DB_TYPE type;
14076 
14077  method_name = node->info.method_call.method_name;
14078  on_call_target = node->info.method_call.on_call_target;
14079  if (on_call_target == NULL)
14080  {
14081  return node;
14082  }
14083 
14084  if (method_name->node_type == PT_NAME)
14085  {
14086  name = method_name->info.name.original;
14087  }
14088 
14089  switch (on_call_target->node_type)
14090  {
14091  case PT_VALUE:
14092  obj = on_call_target->info.value.data_value.op;
14093  if (obj && name)
14094  {
14095  if ((method = (DB_METHOD *) db_get_method (obj, name)) == NULL)
14096  {
14097  if (er_errid () == ER_OBJ_INVALID_METHOD)
14098  {
14099  er_clear ();
14100  }
14101 
14102  method = (DB_METHOD *) db_get_class_method (obj, name);
14103  }
14104  }
14105  break;
14106 
14107  case PT_NAME:
14108  if (on_call_target->data_type && on_call_target->data_type->info.data_type.entity)
14109  {
14110  obj = on_call_target->data_type->info.data_type.entity->info.name.db_object;
14111  }
14112 
14113  if (obj && name)
14114  {
14115  method = (DB_METHOD *) db_get_method (obj, name);
14116  if (method == NULL)
14117  {
14118  if (er_errid () == ER_OBJ_INVALID_METHOD)
14119  {
14120  er_clear ();
14121  }
14122  method = (DB_METHOD *) db_get_class_method (obj, name);
14123  }
14124  }
14125  break;
14126 
14127  default:
14128  break;
14129  }
14130 
14131  if (method)
14132  {
14133  domain = db_method_return_domain (method);
14134  if (domain)
14135  {
14136  type = TP_DOMAIN_TYPE (domain);
14137  node->type_enum = pt_db_to_type_enum (type);
14138  }
14139  }
14140 
14141  return node;
14142 }
14143 
14144 /*
14145  * pt_evaluate_db_value_expr () - apply op to db_value opds & place it in result
14146  * return: 1 if evaluation succeeded, 0 otherwise
14147  * parser(in): handle to the parser context
14148  * expr(in): the expression to be applied
14149  * op(in): a PT_OP_TYPE (the desired operation)
14150  * arg1(in): 1st db_value operand
14151  * arg2(in): 2nd db_value operand
14152  * arg3(in): 3rd db_value operand
14153  * result(out): a newly set db_value if successful, untouched otherwise
14154  * domain(in): domain of result (for arithmetic & set ops)
14155  * o1(in): a PT_NODE containing the source line & column position of arg1
14156  * o2(in): a PT_NODE containing the source line & column position of arg2
14157  * o3(in): a PT_NODE containing the source line & column position of arg3
14158  * qualifier(in): trim qualifier or datetime component specifier
14159  */
14160 
14161 int
14163  DB_VALUE * arg3, DB_VALUE * result, TP_DOMAIN * domain, PT_NODE * o1, PT_NODE * o2,
14164  PT_NODE * o3, PT_MISC_TYPE qualifier)
14165 {
14166  DB_TYPE typ;
14167  DB_TYPE typ1, typ2;
14168  PT_TYPE_ENUM rTyp;
14169  int cmp;
14170  DB_VALUE_COMPARE_RESULT cmp_result;
14171  DB_VALUE_COMPARE_RESULT cmp_result2;
14172  DB_VALUE tmp_val;
14173  int error, i;
14174  DB_DATA_STATUS truncation;
14175  TP_DOMAIN_STATUS dom_status;
14176  PT_NODE *between_ge_lt, *between_ge_lt_arg1, *between_ge_lt_arg2;
14177  DB_VALUE *width_bucket_arg2 = NULL, *width_bucket_arg3 = NULL;
14178 
14179  assert (parser != NULL);
14180 
14181  if (!arg1 || !result)
14182  {
14183  return 0;
14184  }
14185 
14186  typ = TP_DOMAIN_TYPE (domain);
14187  rTyp = pt_db_to_type_enum (typ);
14188 
14189  /* do not coerce arg1, arg2 for STRCAT */
14190  if (op == PT_PLUS && PT_IS_STRING_TYPE (rTyp))
14191  {
14193  {
14194  op = PT_STRCAT;
14195  }
14196  else
14197  {
14198  /* parameters should already be coerced to other types */
14199  assert (false);
14200  }
14201  }
14202 
14203  typ1 = (arg1) ? DB_VALUE_TYPE (arg1) : DB_TYPE_NULL;
14204  typ2 = (arg2) ? DB_VALUE_TYPE (arg2) : DB_TYPE_NULL;
14205  cmp = 0;
14206  db_make_null (result);
14207 
14208  switch (op)
14209  {
14210  case PT_NOT:
14211  if (typ1 == DB_TYPE_NULL)
14212  {
14213  db_make_null (result); /* not NULL = NULL */
14214  }
14215  else if (db_get_int (arg1))
14216  {
14217  db_make_int (result, false); /* not true = false */
14218  }
14219  else
14220  {
14221  db_make_int (result, true); /* not false = true */
14222  }
14223  break;
14224 
14225  case PT_PRIOR:
14226  case PT_QPRIOR:
14227  case PT_CONNECT_BY_ROOT:
14228  if (db_value_clone (arg1, result) != NO_ERROR)
14229  {
14230  return 0;
14231  }
14232  break;
14233 
14235  return 0;
14236 
14237  case PT_BIT_NOT:
14238  switch (typ1)
14239  {
14240  case DB_TYPE_NULL:
14241  db_make_null (result);
14242  break;
14243 
14244  case DB_TYPE_INTEGER:
14245  db_make_bigint (result, ~((DB_BIGINT) db_get_int (arg1)));
14246  break;
14247 
14248  case DB_TYPE_BIGINT:
14249  db_make_bigint (result, ~db_get_bigint (arg1));
14250  break;
14251 
14252  case DB_TYPE_SHORT:
14253  db_make_bigint (result, ~((DB_BIGINT) db_get_short (arg1)));
14254  break;
14255 
14256  default:
14257  return 0;
14258  }
14259  break;
14260 
14261  case PT_BIT_AND:
14262  {
14263  DB_BIGINT bi[2];
14264  DB_TYPE dbtype[2];
14265  DB_VALUE *dbval[2];
14266  int i;
14267 
14268  dbtype[0] = typ1;
14269  dbtype[1] = typ2;
14270  dbval[0] = arg1;
14271  dbval[1] = arg2;
14272 
14273  for (i = 0; i < 2; i++)
14274  {
14275  switch (dbtype[i])
14276  {
14277  case DB_TYPE_NULL:
14278  db_make_null (result);
14279  break;
14280 
14281  case DB_TYPE_INTEGER:
14282  bi[i] = (DB_BIGINT) db_get_int (dbval[i]);
14283  break;
14284 
14285  case DB_TYPE_BIGINT:
14286  bi[i] = db_get_bigint (dbval[i]);
14287  break;
14288 
14289  case DB_TYPE_SHORT:
14290  bi[i] = (DB_BIGINT) db_get_short (dbval[i]);
14291  break;
14292 
14293  default:
14294  return 0;
14295  }
14296  }
14297 
14298  if (dbtype[0] != DB_TYPE_NULL && dbtype[1] != DB_TYPE_NULL)
14299  {
14300  db_make_bigint (result, bi[0] & bi[1]);
14301  }
14302  }
14303  break;
14304 
14305  case PT_BIT_OR:
14306  {
14307  DB_BIGINT bi[2];
14308  DB_TYPE dbtype[2];
14309  DB_VALUE *dbval[2];
14310  int i;
14311 
14312  dbtype[0] = typ1;
14313  dbtype[1] = typ2;
14314  dbval[0] = arg1;
14315  dbval[1] = arg2;
14316 
14317  for (i = 0; i < 2; i++)
14318  {
14319  switch (dbtype[i])
14320  {
14321  case DB_TYPE_NULL:
14322  db_make_null (result);
14323  break;
14324 
14325  case DB_TYPE_INTEGER:
14326  bi[i] = (DB_BIGINT) db_get_int (dbval[i]);
14327  break;
14328 
14329  case DB_TYPE_BIGINT:
14330  bi[i] = db_get_bigint (dbval[i]);
14331  break;
14332 
14333  case DB_TYPE_SHORT:
14334  bi[i] = (DB_BIGINT) db_get_short (dbval[i]);
14335  break;
14336 
14337  default:
14338  return 0;
14339  }
14340  }
14341 
14342  if (dbtype[0] != DB_TYPE_NULL && dbtype[1] != DB_TYPE_NULL)
14343  {
14344  db_make_bigint (result, bi[0] | bi[1]);
14345  }
14346  }
14347  break;
14348 
14349  case PT_BIT_XOR:
14350  {
14351  DB_BIGINT bi[2];
14352  DB_TYPE dbtype[2];
14353  DB_VALUE *dbval[2];
14354  int i;
14355 
14356  dbtype[0] = typ1;
14357  dbtype[1] = typ2;
14358  dbval[0] = arg1;
14359  dbval[1] = arg2;
14360 
14361  for (i = 0; i < 2; i++)
14362  {
14363  switch (dbtype[i])
14364  {
14365  case DB_TYPE_NULL:
14366  db_make_null (result);
14367  break;
14368 
14369  case DB_TYPE_INTEGER:
14370  bi[i] = (DB_BIGINT) db_get_int (dbval[i]);
14371  break;
14372 
14373  case DB_TYPE_BIGINT:
14374  bi[i] = db_get_bigint (dbval[i]);
14375  break;
14376 
14377  case DB_TYPE_SHORT:
14378  bi[i] = (DB_BIGINT) db_get_short (dbval[i]);
14379  break;
14380 
14381  default:
14382  return 0;
14383  }
14384  }
14385 
14386  if (dbtype[0] != DB_TYPE_NULL && dbtype[1] != DB_TYPE_NULL)
14387  {
14388  db_make_bigint (result, bi[0] ^ bi[1]);
14389  }
14390  }
14391  break;
14392 
14393  case PT_BITSHIFT_LEFT:
14394  case PT_BITSHIFT_RIGHT:
14395  {
14396  DB_BIGINT bi[2];
14397  DB_TYPE dbtype[2];
14398  DB_VALUE *dbval[2];
14399  int i;
14400 
14401  dbtype[0] = typ1;
14402  dbtype[1] = typ2;
14403  dbval[0] = arg1;
14404  dbval[1] = arg2;
14405 
14406  for (i = 0; i < 2; i++)
14407  {
14408  switch (dbtype[i])
14409  {
14410  case DB_TYPE_NULL:
14411  db_make_null (result);
14412  break;
14413 
14414  case DB_TYPE_INTEGER:
14415  bi[i] = (DB_BIGINT) db_get_int (dbval[i]);
14416  break;
14417 
14418  case DB_TYPE_BIGINT:
14419  bi[i] = db_get_bigint (dbval[i]);
14420  break;
14421 
14422  case DB_TYPE_SHORT:
14423  bi[i] = (DB_BIGINT) db_get_short (dbval[i]);
14424  break;
14425 
14426  default:
14427  return 0;
14428  }
14429  }
14430 
14431  if (dbtype[0] != DB_TYPE_NULL && dbtype[1] != DB_TYPE_NULL)
14432  {
14433  if (bi[1] < (int) (sizeof (DB_BIGINT) * 8) && bi[1] >= 0)
14434  {
14435  if (op == PT_BITSHIFT_LEFT)
14436  {
14437  db_make_bigint (result, ((UINT64) bi[0]) << ((UINT64) bi[1]));
14438  }
14439  else
14440  {
14441  db_make_bigint (result, ((UINT64) bi[0]) >> ((UINT64) bi[1]));
14442  }
14443  }
14444  else
14445  {
14446  db_make_bigint (result, 0);
14447  }
14448  }
14449  }
14450  break;
14451 
14452  case PT_DIV:
14453  case PT_MOD:
14454  {
14455  DB_BIGINT bi[2];
14456  DB_TYPE dbtype[2];
14457  DB_VALUE *dbval[2];
14458  int i;
14459 
14460  dbtype[0] = typ1;
14461  dbtype[1] = typ2;
14462  dbval[0] = arg1;
14463  dbval[1] = arg2;
14464 
14465  for (i = 0; i < 2; i++)
14466  {
14467  switch (dbtype[i])
14468  {
14469  case DB_TYPE_NULL:
14470  db_make_null (result);
14471  break;
14472 
14473  case DB_TYPE_INTEGER:
14474  bi[i] = (DB_BIGINT) db_get_int (dbval[i]);
14475  break;
14476 
14477  case DB_TYPE_BIGINT:
14478  bi[i] = db_get_bigint (dbval[i]);
14479  break;
14480 
14481  case DB_TYPE_SHORT:
14482  bi[i] = (DB_BIGINT) db_get_short (dbval[i]);
14483  break;
14484 
14485  default:
14486  return 0;
14487  }
14488  }
14489 
14490  if (dbtype[0] != DB_TYPE_NULL && dbtype[1] != DB_TYPE_NULL)
14491  {
14492  if (bi[1] != 0)
14493  {
14494  if (op == PT_DIV)
14495  {
14496  if (typ1 == DB_TYPE_INTEGER)
14497  {
14498  if (OR_CHECK_INT_DIV_OVERFLOW (bi[0], bi[1]))
14499  {
14500  goto overflow;
14501  }
14502  db_make_int (result, (INT32) (bi[0] / bi[1]));
14503  }
14504  else if (typ1 == DB_TYPE_BIGINT)
14505  {
14506  if (OR_CHECK_BIGINT_DIV_OVERFLOW (bi[0], bi[1]))
14507  {
14508  goto overflow;
14509  }
14510  db_make_bigint (result, bi[0] / bi[1]);
14511  }
14512  else
14513  {
14514  if (OR_CHECK_SHORT_DIV_OVERFLOW (bi[0], bi[1]))
14515  {
14516  goto overflow;
14517  }
14518  db_make_short (result, (INT16) (bi[0] / bi[1]));
14519  }
14520  }
14521  else
14522  {
14523  if (typ1 == DB_TYPE_INTEGER)
14524  {
14525  db_make_int (result, (INT32) (bi[0] % bi[1]));
14526  }
14527  else if (typ1 == DB_TYPE_BIGINT)
14528  {
14529  db_make_bigint (result, bi[0] % bi[1]);
14530  }
14531  else
14532  {
14533  db_make_short (result, (INT16) (bi[0] % bi[1]));
14534  }
14535  }
14536  }
14537  else
14538  {
14540  return 0;
14541  }
14542  }
14543  }
14544  break;
14545 
14546  case PT_IF:
14547  { /* Obs: when this case occurs both args are the same type */
14548  if (DB_IS_NULL (arg1))
14549  {
14550  if (db_value_clone (arg3, result) != NO_ERROR)
14551  {
14552  return 0;
14553  }
14554  }
14555  else
14556  {
14557  if (db_get_int (arg1))
14558  {
14559  if (db_value_clone (arg2, result) != NO_ERROR)
14560  {
14561  return 0;
14562  }
14563  }
14564  else
14565  {
14566  if (db_value_clone (arg3, result) != NO_ERROR)
14567  {
14568  return 0;
14569  }
14570  }
14571  }
14572  }
14573  break;
14574 
14575  case PT_IFNULL:
14576  case PT_COALESCE:
14577  case PT_NVL:
14578  {
14579  DB_VALUE *src;
14580  TP_DOMAIN *target_domain = NULL;
14581  PT_NODE *target_node;
14582 
14583  if (typ == DB_TYPE_VARIABLE)
14584  {
14585  TP_DOMAIN *arg1_domain, *arg2_domain;
14586  TP_DOMAIN tmp_arg1_domain, tmp_arg2_domain;
14587 
14588  arg1_domain = tp_domain_resolve_value (arg1, &tmp_arg1_domain);
14589  arg2_domain = tp_domain_resolve_value (arg2, &tmp_arg2_domain);
14590 
14591  target_domain = tp_infer_common_domain (arg1_domain, arg2_domain);
14592  }
14593  else
14594  {
14595  target_domain = domain;
14596  }
14597 
14598  if (typ1 == DB_TYPE_NULL)
14599  {
14600  src = arg2;
14601  target_node = o2;
14602  }
14603  else
14604  {
14605  src = arg1;
14606  target_node = o1;
14607  }
14608 
14609  if (tp_value_cast (src, result, target_domain, false) != DOMAIN_COMPATIBLE)
14610  {
14611  rTyp = pt_db_to_type_enum (target_domain->type->id);
14613  pt_short_print (parser, target_node), pt_show_type_enum (rTyp));
14614 
14615  return 0;
14616  }
14617  }
14618  return 1;
14619 
14620  case PT_NVL2:
14621  {
14622  DB_VALUE *src;
14623  PT_NODE *target_node;
14624  TP_DOMAIN *target_domain = NULL;
14625  TP_DOMAIN *tmp_domain = NULL;
14626 
14627  if (typ == DB_TYPE_VARIABLE)
14628  {
14629  TP_DOMAIN *arg1_domain, *arg2_domain, *arg3_domain;
14630  TP_DOMAIN tmp_arg1_domain, tmp_arg2_domain, tmp_arg3_domain;
14631 
14632  arg1_domain = tp_domain_resolve_value (arg1, &tmp_arg1_domain);
14633  arg2_domain = tp_domain_resolve_value (arg2, &tmp_arg2_domain);
14634  arg3_domain = tp_domain_resolve_value (arg3, &tmp_arg3_domain);
14635 
14636  tmp_domain = tp_infer_common_domain (arg1_domain, arg2_domain);
14637  target_domain = tp_infer_common_domain (tmp_domain, arg3_domain);
14638  }
14639  else
14640  {
14641  target_domain = domain;
14642  }
14643 
14644  if (typ1 == DB_TYPE_NULL)
14645  {
14646  src = arg3;
14647  target_node = o3;
14648  }
14649  else
14650  {
14651  src = arg2;
14652  target_node = o2;
14653  }
14654 
14655  if (tp_value_cast (src, result, target_domain, false) != DOMAIN_COMPATIBLE)
14656  {
14657  rTyp = pt_db_to_type_enum (target_domain->type->id);
14659  pt_short_print (parser, target_node), pt_show_type_enum (rTyp));
14660  return 0;
14661  }
14662  }
14663  return 1;
14664 
14665  case PT_ISNULL:
14666  if (DB_IS_NULL (arg1))
14667  {
14668  db_make_int (result, true);
14669  }
14670  else
14671  {
14672  db_make_int (result, false);
14673  }
14674  break;
14675 
14676  case PT_UNARY_MINUS:
14677  switch (typ1)
14678  {
14679  case DB_TYPE_NULL:
14680  db_make_null (result); /* -NA = NA, -NULL = NULL */
14681  break;
14682 
14683  case DB_TYPE_INTEGER:
14684  if (db_get_int (arg1) == DB_INT32_MIN)
14685  {
14686  goto overflow;
14687  }
14688  else
14689  {
14690  db_make_int (result, -db_get_int (arg1));
14691  }
14692  break;
14693 
14694  case DB_TYPE_BIGINT:
14695  if (db_get_bigint (arg1) == DB_BIGINT_MIN)
14696  {
14697  goto overflow;
14698  }
14699  else
14700  {
14701  db_make_bigint (result, -db_get_bigint (arg1));
14702  }
14703  break;
14704 
14705  case DB_TYPE_SHORT:
14706  if (db_get_short (arg1) == DB_INT16_MIN)
14707  {
14708  goto overflow;
14709  }
14710  else
14711  {
14712  db_make_short (result, -db_get_short (arg1));
14713  }
14714  break;
14715 
14716  case DB_TYPE_FLOAT:
14717  db_make_float (result, -db_get_float (arg1));
14718  break;
14719 
14720  case DB_TYPE_DOUBLE:
14721  db_make_double (result, -db_get_double (arg1));
14722  break;
14723 
14724  case DB_TYPE_NUMERIC:
14725  if (numeric_db_value_negate (arg1) != NO_ERROR)
14726  {
14727  PT_ERRORc (parser, o1, er_msg ());
14728  return 0;
14729  }
14730 
14731  db_make_numeric (result, db_get_numeric (arg1), DB_VALUE_PRECISION (arg1), DB_VALUE_SCALE (arg1));
14732  break;
14733 
14734  case DB_TYPE_MONETARY:
14735  db_make_monetary (result, DB_CURRENCY_DEFAULT, -db_get_monetary (arg1)->amount);
14736  break;
14737 
14738  case DB_TYPE_CHAR:
14739  case DB_TYPE_VARCHAR:
14740  case DB_TYPE_NCHAR:
14741  case DB_TYPE_VARNCHAR:
14743  db_make_null (&tmp_val);
14744  /* force explicit cast ; scenario : INSERT INTO t VALUE(-?) , USING '10', column is INTEGER */
14745  if (tp_value_cast (arg1, &tmp_val, domain, false) != DOMAIN_COMPATIBLE)
14746  {
14748  {
14751  return 0;
14752  }
14753  else
14754  {
14755  db_make_null (result);
14756  er_clear ();
14757  return 1;
14758  }
14759  }
14760  else
14761  {
14762  pr_clear_value (arg1);
14763  *arg1 = tmp_val;
14764  db_make_double (result, -db_get_double (arg1));
14765  }
14766  break;
14767 
14768  default:
14769  return 0; /* an unhandled type is a failure */
14770  }
14771  break;
14772 
14773  case PT_IS_NULL:
14774  if (typ1 == DB_TYPE_NULL)
14775  {
14776  db_make_int (result, true);
14777  }
14778  else
14779  {
14780  db_make_int (result, false);
14781  }
14782  break;
14783 
14784  case PT_IS_NOT_NULL:
14785  if (typ1 == DB_TYPE_NULL)
14786  {
14787  db_make_int (result, false);
14788  }
14789  else
14790  {
14791  db_make_int (result, true);
14792  }
14793  break;
14794 
14795  case PT_IS:
14796  case PT_IS_NOT:
14797  {
14798  int _true, _false;
14799 
14800  _true = (op == PT_IS) ? 1 : 0;
14801  _false = 1 - _true;
14802 
14803  if ((o1 && o1->node_type != PT_VALUE) || (o2 && o2->node_type != PT_VALUE))
14804  {
14805  return 0;
14806  }
14807  if (DB_IS_NULL (arg1))
14808  {
14809  if (DB_IS_NULL (arg2))
14810  {
14811  db_make_int (result, _true);
14812  }
14813  else
14814  {
14815  db_make_int (result, _false);
14816  }
14817  }
14818  else
14819  {
14820  if (DB_IS_NULL (arg2))
14821  {
14822  db_make_int (result, _false);
14823  }
14824  else
14825  {
14826  if (db_get_int (arg1) == db_get_int (arg2))
14827  {
14828  db_make_int (result, _true);
14829  }
14830  else
14831  {
14832  db_make_int (result, _false);
14833  }
14834  }
14835  }
14836  }
14837  break;
14838 
14839  case PT_TYPEOF:
14840  if (db_typeof_dbval (result, arg1) != NO_ERROR)
14841  {
14842  db_make_null (result);
14843  }
14844  break;
14845  case PT_CONCAT_WS:
14846  if (DB_VALUE_TYPE (arg3) == DB_TYPE_NULL)
14847  {
14848  db_make_null (result);
14849  break;
14850  }
14851  /* no break here */
14852  case PT_CONCAT:
14853  if (typ1 == DB_TYPE_NULL || (typ2 == DB_TYPE_NULL && o2))
14854  {
14855  bool check_empty_string;
14856  check_empty_string = (prm_get_bool_value (PRM_ID_ORACLE_STYLE_EMPTY_STRING)) ? true : false;
14857 
14858  if (!check_empty_string || !PT_IS_STRING_TYPE (rTyp))
14859  {
14860  if (op != PT_CONCAT_WS)
14861  {
14862  db_make_null (result);
14863  break;
14864  }
14865  }
14866  }
14867 
14868  /* screen out cases we don't evaluate */
14869  if (!PT_IS_STRING_TYPE (rTyp))
14870  {
14871  return 0;
14872  }
14873 
14874  switch (typ)
14875  {
14876  case DB_TYPE_CHAR:
14877  case DB_TYPE_NCHAR:
14878  case DB_TYPE_VARCHAR:
14879  case DB_TYPE_VARNCHAR:
14880  case DB_TYPE_BIT:
14881  case DB_TYPE_VARBIT:
14882  if (o2)
14883  {
14884  if (op == PT_CONCAT_WS)
14885  {
14886  if (typ1 == DB_TYPE_NULL)
14887  {
14888  if (db_value_clone (arg2, result) != NO_ERROR)
14889  {
14890  return 0;
14891  }
14892  }
14893  else if (typ2 == DB_TYPE_NULL)
14894  {
14895  if (db_value_clone (arg1, result) != NO_ERROR)
14896  {
14897  return 0;
14898  }
14899  }
14900  else
14901  {
14902  if (db_string_concatenate (arg1, arg3, &tmp_val, &truncation) < 0 || truncation != DATA_STATUS_OK)
14903  {
14904  PT_ERRORc (parser, o1, er_msg ());
14905  return 0;
14906  }
14907  if (db_string_concatenate (&tmp_val, arg2, result, &truncation) < 0
14908  || truncation != DATA_STATUS_OK)
14909  {
14910  PT_ERRORc (parser, o1, er_msg ());
14911  return 0;
14912  }
14913  }
14914  }
14915  else
14916  {
14917  if (db_string_concatenate (arg1, arg2, result, &truncation) < 0 || truncation != DATA_STATUS_OK)
14918  {
14919  PT_ERRORc (parser, o1, er_msg ());
14920  return 0;
14921  }
14922  }
14923  }
14924  else
14925  {
14926  if (db_value_clone (arg1, result) != NO_ERROR)
14927  {
14928  return 0;
14929  }
14930  }
14931  break;
14932 
14933  default:
14934  return 0;
14935  }
14936 
14937  break;
14938 
14939  case PT_FIELD:
14940  if (o1->node_type != PT_VALUE || (o2 && o2->node_type != PT_VALUE) || o3->node_type != PT_VALUE)
14941  {
14942  return 0;
14943  }
14944 
14945  if (DB_IS_NULL (arg3))
14946  {
14947  db_make_int (result, 0);
14948  break;
14949  }
14950 
14951  if (o3 && o3->next && o3->next->info.value.data_value.i == 1)
14952  {
14953  if (tp_value_compare (arg3, arg1, 1, 0) == DB_EQ)
14954  {
14955  db_make_int (result, 1);
14956  }
14957  else if (tp_value_compare (arg3, arg2, 1, 0) == DB_EQ)
14958  {
14959  db_make_int (result, 2);
14960  }
14961  else
14962  {
14963  db_make_int (result, 0);
14964  }
14965  }
14966  else
14967  {
14968  i = db_get_int (arg1);
14969  if (i > 0)
14970  {
14971  db_make_int (result, i);
14972  }
14973  else
14974  {
14975  if (tp_value_compare (arg3, arg2, 1, 0) == DB_EQ)
14976  {
14977  if (o3 && o3->next)
14978  {
14979  db_make_int (result, o3->next->info.value.data_value.i);
14980  }
14981  }
14982  else
14983  {
14984  db_make_int (result, 0);
14985  }
14986  }
14987  }
14988  break;
14989 
14990  case PT_LEFT:
14991  if (!DB_IS_NULL (arg1) && !DB_IS_NULL (arg2))
14992  {
14993  DB_VALUE tmp_val2;
14994  if (tp_value_coerce (arg2, &tmp_val2, &tp_Integer_domain) != DOMAIN_COMPATIBLE)
14995  {
14998  return 0;
14999  }
15000 
15001  db_make_int (&tmp_val, 0);
15002  error = db_string_substring (SUBSTRING, arg1, &tmp_val, &tmp_val2, result);
15003  if (error < 0)
15004  {
15005  PT_ERRORc (parser, o1, er_msg ());
15006  return 0;
15007  }
15008  else
15009  {
15010  return 1;
15011  }
15012  }
15013  else
15014  {
15015  db_make_null (result);
15016  return 1;
15017  }
15018 
15019  case PT_RIGHT:
15020  if (!DB_IS_NULL (arg1) && !DB_IS_NULL (arg2))
15021  {
15022  DB_VALUE tmp_val2;
15023 
15024  if (QSTR_IS_BIT (typ1))
15025  {
15026  if (db_string_bit_length (arg1, &tmp_val) != NO_ERROR)
15027  {
15028  PT_ERRORc (parser, o1, er_msg ());
15029  return 0;
15030  }
15031  }
15032  else
15033  {
15034  if (db_string_char_length (arg1, &tmp_val) != NO_ERROR)
15035  {
15036  PT_ERRORc (parser, o1, er_msg ());
15037  return 0;
15038  }
15039  }
15040  if (DB_IS_NULL (&tmp_val))
15041  {
15042  PT_ERRORc (parser, o1, er_msg ());
15043  return 0;
15044  }
15045 
15046  if (tp_value_coerce (arg2, &tmp_val2, &tp_Integer_domain) != DOMAIN_COMPATIBLE)
15047  {
15050  return 0;
15051  }
15052 
15053  /* If len, defined as second argument, is negative value, RIGHT function returns the entire string. It's same
15054  * behavior with LEFT and SUBSTRING. */
15055  if (db_get_int (&tmp_val2) < 0)
15056  {
15057  db_make_int (&tmp_val, 0);
15058  }
15059  else
15060  {
15061  db_make_int (&tmp_val, db_get_int (&tmp_val) - db_get_int (&tmp_val2) + 1);
15062  }
15063  error = db_string_substring (SUBSTRING, arg1, &tmp_val, &tmp_val2, result);
15064  if (error < 0)
15065  {
15066  PT_ERRORc (parser, o1, er_msg ());
15067  return 0;
15068  }
15069  else
15070  {
15071  return 1;
15072  }
15073  }
15074  else
15075  {
15076  db_make_null (result);
15077  return 1;
15078  }
15079 
15080  case PT_REPEAT:
15081  {
15082  if (!DB_IS_NULL (arg1) && !DB_IS_NULL (arg2))
15083  {
15084  error = db_string_repeat (arg1, arg2, result);
15085  if (error < 0)
15086  {
15087  PT_ERRORc (parser, o1, er_msg ());
15088  return 0;
15089  }
15090  else
15091  {
15092  return 1;
15093  }
15094  }
15095  else
15096  {
15097  db_make_null (result);
15098  return 1;
15099  }
15100  }
15101  /* break is not needed because of return(s) */
15102  case PT_SPACE:
15103  {
15104  if (DB_IS_NULL (arg1))
15105  {
15106  db_make_null (result);
15107  return 1;
15108  }
15109  else
15110  {
15111  error = db_string_space (arg1, result);
15112  if (error < 0)
15113  {
15114  PT_ERRORc (parser, o1, er_msg ());
15115  return 0;
15116  }
15117  else
15118  {
15119  return 1;
15120  }
15121  }
15122  }
15123  break;
15124 
15125  case PT_LOCATE:
15126  if (DB_IS_NULL (arg1) || DB_IS_NULL (arg2) || (o3 && DB_IS_NULL (arg3)))
15127  {
15128  db_make_null (result);
15129  }
15130  else
15131  {
15132  if (!o3)
15133  {
15134  if (db_string_position (arg1, arg2, result) != NO_ERROR)
15135  {
15136  PT_ERRORc (parser, o1, er_msg ());
15137  return 0;
15138  }
15139  }
15140  else
15141  {
15142  DB_VALUE tmp_len, tmp_arg3;
15143  int tmp = db_get_int (arg3);
15144  if (tmp < 1)
15145  {
15146  db_make_int (&tmp_arg3, 1);
15147  }
15148  else
15149  {
15150  db_make_int (&tmp_arg3, tmp);
15151  }
15152 
15153  if (db_string_char_length (arg2, &tmp_len) != NO_ERROR)
15154  {
15155  PT_ERRORc (parser, o2, er_msg ());
15156  return 0;
15157  }
15158  if (DB_IS_NULL (&tmp_len))
15159  {
15160  PT_ERRORc (parser, o2, er_msg ());
15161  return 0;
15162  }
15163 
15164  db_make_int (&tmp_len, db_get_int (&tmp_len) - db_get_int (&tmp_arg3) + 1);
15165 
15166  if (db_string_substring (SUBSTRING, arg2, &tmp_arg3, &tmp_len, &tmp_val) != NO_ERROR)
15167  {
15168  PT_ERRORc (parser, o2, er_msg ());
15169  return 0;
15170  }
15171 
15172  if (db_string_position (arg1, &tmp_val, result) != NO_ERROR)
15173  {
15174  PT_ERRORc (parser, o1, er_msg ());
15175  return 0;
15176  }
15177  if (db_get_int (result) > 0)
15178  {
15179  db_make_int (result, db_get_int (result) + db_get_int (&tmp_arg3) - 1);
15180  }
15181  }
15182  }
15183  break;
15184 
15185  case PT_MID:
15186  if (DB_IS_NULL (arg1) || DB_IS_NULL (arg2) || DB_IS_NULL (arg3))
15187  {
15188  db_make_null (result);
15189  }
15190  else
15191  {
15192  DB_VALUE tmp_len, tmp_arg2, tmp_arg3;
15193  int pos, len;
15194 
15195  pos = db_get_int (arg2);
15196  len = db_get_int (arg3);
15197 
15198  if (pos < 0)
15199  {
15200  if (QSTR_IS_BIT (typ1))
15201  {
15202  if (db_string_bit_length (arg1, &tmp_len) != NO_ERROR)
15203  {
15204  PT_ERRORc (parser, o1, er_msg ());
15205  return 0;
15206  }
15207  }
15208  else
15209  {
15210  if (db_string_char_length (arg1, &tmp_len) != NO_ERROR)
15211  {
15212  PT_ERRORc (parser, o1, er_msg ());
15213  return 0;
15214  }
15215  }
15216  if (DB_IS_NULL (&tmp_len))
15217  {
15218  PT_ERRORc (parser, o1, er_msg ());
15219  return 0;
15220  }
15221  pos = pos + db_get_int (&tmp_len) + 1;
15222  }
15223 
15224  if (pos < 1)
15225  {
15226  db_make_int (&tmp_arg2, 1);
15227  }
15228  else
15229  {
15230  db_make_int (&tmp_arg2, pos);
15231  }
15232 
15233  if (len < 1)
15234  {
15235  db_make_int (&tmp_arg3, 0);
15236  }
15237  else
15238  {
15239  db_make_int (&tmp_arg3, len);
15240  }
15241 
15242  error = db_string_substring (SUBSTRING, arg1, &tmp_arg2, &tmp_arg3, result);
15243  if (error < 0)
15244  {
15245  PT_ERRORc (parser, o1, er_msg ());
15246  return 0;
15247  }
15248  else
15249  {
15250  return 1;
15251  }
15252  }
15253  break;
15254 
15255  case PT_STRCMP:
15256  if (DB_IS_NULL (arg1) || DB_IS_NULL (arg2))
15257  {
15258  db_make_null (result);
15259  }
15260  else
15261  {
15262  if (db_string_compare (arg1, arg2, result) != NO_ERROR)
15263  {
15264  PT_ERRORc (parser, o1, er_msg ());
15265  return 0;
15266  }
15267 
15268  cmp = db_get_int (result);
15269  if (cmp < 0)
15270  {
15271  cmp = -1;
15272  }
15273  else if (cmp > 0)
15274  {
15275  cmp = 1;
15276  }
15277  db_make_int (result, cmp);
15278  }
15279  break;
15280 
15281  case PT_REVERSE:
15282  if (DB_IS_NULL (arg1))
15283  {
15284  db_make_null (result);
15285  }
15286  else
15287  {
15288  if (db_string_reverse (arg1, result) != NO_ERROR)
15289  {
15290  PT_ERRORc (parser, o1, er_msg ());
15291  return 0;
15292  }
15293  }
15294  break;
15295 
15296  case PT_DISK_SIZE:
15297  if (DB_IS_NULL (arg1))
15298  {
15299  db_make_int (result, 0);
15300  }
15301  else
15302  {
15303  db_make_int (result, pr_data_writeval_disk_size (arg1));
15304  /* call pr_data_writeval_disk_size function to return the size on disk */
15305  }
15306  break;
15307 
15308  case PT_BIT_COUNT:
15309  if (db_bit_count_dbval (result, arg1) != NO_ERROR)
15310  {
15311  PT_ERRORc (parser, o1, er_msg ());
15312  return 0;
15313  }
15314  break;
15315 
15316  case PT_EXISTS:
15317  if (TP_IS_SET_TYPE (typ1))
15318  {
15319  if (db_set_size (db_get_set (arg1)) > 0)
15320  {
15321  db_make_int (result, true);
15322  }
15323  else
15324  {
15325  db_make_int (result, false);
15326  }
15327  }
15328  else
15329  {
15330  db_make_int (result, true);
15331  }
15332  break;
15333 
15334  case PT_AND:
15335  if ((typ1 == DB_TYPE_NULL && typ2 == DB_TYPE_NULL) || (typ1 == DB_TYPE_NULL && db_get_int (arg2))
15336  || (typ2 == DB_TYPE_NULL && db_get_int (arg1)))
15337  {
15338  db_make_null (result);
15339  }
15340  else if (typ1 != DB_TYPE_NULL && db_get_int (arg1) && typ2 != DB_TYPE_NULL && db_get_int (arg2))
15341  {
15342  db_make_int (result, true);
15343  }
15344  else
15345  {
15346  db_make_int (result, false);
15347  }
15348  break;
15349 
15350  case PT_OR:
15351  if ((typ1 == DB_TYPE_NULL && typ2 == DB_TYPE_NULL) || (typ1 == DB_TYPE_NULL && !db_get_int (arg2))
15352  || (typ2 == DB_TYPE_NULL && !db_get_int (arg1)))
15353  {
15354  db_make_null (result);
15355  }
15356  else if (typ1 != DB_TYPE_NULL && !db_get_int (arg1) && typ2 != DB_TYPE_NULL && !db_get_int (arg2))
15357  {
15358  db_make_int (result, false);
15359  }
15360  else
15361  {
15362  db_make_int (result, true);
15363  }
15364  break;
15365 
15366  case PT_XOR:
15367  if (typ1 == DB_TYPE_NULL || typ2 == DB_TYPE_NULL)
15368  {
15369  db_make_null (result);
15370  }
15371  else if ((!db_get_int (arg1) && !db_get_int (arg2)) || (db_get_int (arg1) && db_get_int (arg2)))
15372  {
15373  db_make_int (result, false);
15374  }
15375  else
15376  {
15377  db_make_int (result, true);
15378  }
15379  break;
15380 
15381  case PT_PLUS:
15382  case PT_MINUS:
15383  case PT_TIMES:
15384  case PT_DIVIDE:
15385  if (typ == DB_TYPE_VARIABLE && pt_is_op_hv_late_bind (op))
15386  {
15387  rTyp = pt_common_type (pt_db_to_type_enum (typ1), pt_db_to_type_enum (typ2));
15388  /* TODO: override common type for plus, minus this should be done in a more generic code : but,
15389  * pt_common_type_op is expected to return TYPE_NONE in this case */
15390  if (PT_IS_CHAR_STRING_TYPE (rTyp) && (op != PT_PLUS || prm_get_bool_value (PRM_ID_PLUS_AS_CONCAT) == false))
15391  {
15392  rTyp = PT_TYPE_DOUBLE;
15393  }
15394  typ = pt_type_enum_to_db (rTyp);
15395  domain = tp_domain_resolve_default (typ);
15396  }
15397 
15398  if (typ1 == DB_TYPE_NULL || typ2 == DB_TYPE_NULL)
15399  {
15400  bool check_empty_string;
15401 
15402  check_empty_string = ((prm_get_bool_value (PRM_ID_ORACLE_STYLE_EMPTY_STRING)) ? true : false);
15403  if (!check_empty_string || op != PT_PLUS || !PT_IS_STRING_TYPE (rTyp))
15404  {
15405  db_make_null (result); /* NULL arith_op any = NULL */
15406  break;
15407  }
15408  }
15409 
15410  /* screen out cases we don't evaluate */
15411  if (!PT_IS_NUMERIC_TYPE (rTyp) && !PT_IS_STRING_TYPE (rTyp) && rTyp != PT_TYPE_SET && rTyp != PT_TYPE_MULTISET
15412  && rTyp != PT_TYPE_SEQUENCE && !PT_IS_DATE_TIME_TYPE (rTyp))
15413  {
15414  return 0;
15415  }
15416 
15417  /* don't coerce dates and times */
15418  if (!TP_IS_DATE_OR_TIME_TYPE (typ)
15419  && !((typ == DB_TYPE_INTEGER || typ == DB_TYPE_BIGINT) && (TP_IS_DATE_OR_TIME_TYPE (typ1) && typ1 == typ2)))
15420  {
15421  /* coerce operands to data type of result */
15422  if (typ1 != typ)
15423  {
15424  db_make_null (&tmp_val);
15425  /* force explicit cast ; scenario : INSERT INTO t VALUE(''1''+?) , USING 10, column is INTEGER */
15426  if (tp_value_cast (arg1, &tmp_val, domain, false) != DOMAIN_COMPATIBLE)
15427  {
15430 
15431  return 0;
15432  }
15433  else
15434  {
15435  pr_clear_value (arg1);
15436  *arg1 = tmp_val;
15437  }
15438  }
15439 
15440  if (typ2 != typ)
15441  {
15442  db_make_null (&tmp_val);
15443  /* force explicit cast ; scenario: INSERT INTO t VALUE(? + ''1'') , USING 10, column is INTEGER */
15444  if (tp_value_cast (arg2, &tmp_val, domain, false) != DOMAIN_COMPATIBLE)
15445  {
15448 
15449  return 0;
15450  }
15451  else
15452  {
15453  pr_clear_value (arg2);
15454  *arg2 = tmp_val;
15455  }
15456  }
15457  }
15458 
15459  switch (op)
15460  {
15461  case PT_PLUS:
15462  switch (typ)
15463  {
15464  case DB_TYPE_SET:
15465  case DB_TYPE_MULTISET:
15466  case DB_TYPE_SEQUENCE:
15467  if (!pt_union_sets (parser, domain, arg1, arg2, result, o2))
15468  {
15469  return 0; /* set union failed */
15470  }
15471  break;
15472 
15473  case DB_TYPE_CHAR:
15474  case DB_TYPE_NCHAR:
15475  case DB_TYPE_VARCHAR:
15476  case DB_TYPE_VARNCHAR:
15477  case DB_TYPE_BIT:
15478  case DB_TYPE_VARBIT:
15479  if (db_string_concatenate (arg1, arg2, result, &truncation) < 0 || truncation != DATA_STATUS_OK)
15480  {
15481  PT_ERRORc (parser, o1, er_msg ());
15482  return 0;
15483  }
15484  break;
15485 
15486  case DB_TYPE_INTEGER:
15487  {
15488  int i1, i2, itmp;
15489 
15490  i1 = db_get_int (arg1);
15491  i2 = db_get_int (arg2);
15492  itmp = i1 + i2;
15493  if (OR_CHECK_ADD_OVERFLOW (i1, i2, itmp))
15494  goto overflow;
15495  else
15496  db_make_int (result, itmp);
15497  break;
15498  }
15499 
15500  case DB_TYPE_BIGINT:
15501  {
15502  DB_BIGINT bi1, bi2, bitmp;
15503 
15504  bi1 = db_get_bigint (arg1);
15505  bi2 = db_get_bigint (arg2);
15506  bitmp = bi1 + bi2;
15507  if (OR_CHECK_ADD_OVERFLOW (bi1, bi2, bitmp))
15508  goto overflow;
15509  else
15510  db_make_bigint (result, bitmp);
15511  break;
15512  }
15513 
15514  case DB_TYPE_SHORT:
15515  {
15516  short s1, s2, stmp;
15517 
15518  s1 = db_get_short (arg1);
15519  s2 = db_get_short (arg2);
15520  stmp = s1 + s2;
15521  if (OR_CHECK_ADD_OVERFLOW (s1, s2, stmp))
15522  goto overflow;
15523  else
15524  db_make_short (result, stmp);
15525  break;
15526  }
15527 
15528  case DB_TYPE_FLOAT:
15529  {
15530  float ftmp;
15531 
15532  ftmp = db_get_float (arg1) + db_get_float (arg2);
15533  if (OR_CHECK_FLOAT_OVERFLOW (ftmp))
15534  goto overflow;
15535  else
15536  db_make_float (result, ftmp);
15537  break;
15538  }
15539 
15540  case DB_TYPE_DOUBLE:
15541  {
15542  double dtmp;
15543 
15544  dtmp = db_get_double (arg1) + db_get_double (arg2);
15545  if (OR_CHECK_DOUBLE_OVERFLOW (dtmp))
15546  goto overflow;
15547  else
15548  db_make_double (result, dtmp);
15549  break;
15550  }
15551 
15552  case DB_TYPE_NUMERIC:
15553  if (numeric_db_value_add (arg1, arg2, result) != NO_ERROR)
15554  {
15555  PT_ERRORc (parser, o1, er_msg ());
15556  return 0;
15557  }
15558 
15559  dom_status = tp_value_coerce (result, result, domain);
15560  if (dom_status != DOMAIN_COMPATIBLE)
15561  {
15562  (void) tp_domain_status_er_set (dom_status, ARG_FILE_LINE, result, domain);
15563  return 0;
15564  }
15565  break;
15566 
15567  case DB_TYPE_MONETARY:
15568  {
15569  double dtmp;
15570 
15571  dtmp = (db_get_monetary (arg1)->amount + db_get_monetary (arg2)->amount);
15572  if (OR_CHECK_DOUBLE_OVERFLOW (dtmp))
15573  goto overflow;
15574  else
15575  db_make_monetary (result, DB_CURRENCY_DEFAULT, dtmp);
15576  break;
15577  }
15578 
15579  case DB_TYPE_TIME:
15580  {
15581  DB_TIME time, result_time;
15582  int hour, minute, second;
15583  DB_BIGINT itmp;
15584  DB_VALUE *other;
15585 
15586  if (DB_VALUE_TYPE (arg1) == DB_TYPE_TIME)
15587  {
15588  time = *db_get_time (arg1);
15589  other = arg2;
15590  }
15591  else
15592  {
15593  time = *db_get_time (arg2);
15594  other = arg1;
15595  }
15596 
15597  switch (DB_VALUE_TYPE (other))
15598  {
15599  case DB_TYPE_INTEGER:
15600  itmp = db_get_int (other); /* SECONDS_OF_ONE_DAY */
15601  break;
15602  case DB_TYPE_SMALLINT:
15603  itmp = db_get_short (other); /* SECONDS_OF_ONE_DAY */
15604  break;
15605  case DB_TYPE_BIGINT:
15606  itmp = db_get_bigint (other); /* SECONDS_OF_ONE_DAY */
15607  break;
15608  default:
15609  return 0;
15610  }
15611  if (itmp < 0)
15612  {
15613  DB_TIME uother = (DB_TIME) ((-itmp) % 86400);
15614  if (time < uother)
15615  {
15616  time += 86400;
15617  }
15618  result_time = time - uother;
15619  }
15620  else
15621  {
15622  result_time = (itmp + time) % 86400;
15623  }
15624  db_time_decode (&result_time, &hour, &minute, &second);
15625  db_make_time (result, hour, minute, second);
15626  }
15627  break;
15628 
15629  case DB_TYPE_TIMESTAMP:
15630  case DB_TYPE_TIMESTAMPLTZ:
15631  {
15632  DB_UTIME *utime, result_utime;
15633  DB_VALUE *other;
15634  DB_BIGINT bi;
15635 
15637  {
15638  utime = db_get_timestamp (arg1);
15639  other = arg2;
15640  }
15641  else
15642  {
15643  utime = db_get_timestamp (arg2);
15644  other = arg1;
15645  }
15646 
15647  if (*utime == 0)
15648  {
15649  /* operation with zero date returns null */
15650  db_make_null (result);
15652  {
15654  goto error_zerodate;
15655  }
15656  break;
15657  }
15658 
15659  switch (DB_VALUE_TYPE (other))
15660  {
15661  case DB_TYPE_INTEGER:
15662  bi = db_get_int (other);
15663  break;
15664  case DB_TYPE_SMALLINT:
15665  bi = db_get_short (other);
15666  break;
15667  case DB_TYPE_BIGINT:
15668  bi = db_get_bigint (other);
15669  break;
15670  default:
15671  return 0;
15672  }
15673 
15674  if (bi < 0)
15675  {
15676  if (bi == DB_BIGINT_MIN)
15677  {
15678  if (*utime == 0)
15679  {
15680  goto overflow;
15681  }
15682  else
15683  {
15684  bi++;
15685  (*utime)--;
15686  }
15687  }
15688  if (OR_CHECK_UNS_SUB_UNDERFLOW (*utime, -bi, (*utime) + bi))
15689  {
15690  goto overflow;
15691  }
15692  result_utime = (DB_UTIME) ((*utime) + bi);
15693  }
15694  else
15695  {
15696  result_utime = (DB_UTIME) (*utime + bi);
15697  if (OR_CHECK_UNS_ADD_OVERFLOW (*utime, bi, result_utime) || INT_MAX < result_utime)
15698  {
15699  goto overflow;
15700  }
15701  }
15702 
15703  if (typ == DB_TYPE_TIMESTAMPLTZ)
15704  {
15705  db_make_timestampltz (result, result_utime);
15706  }
15707  else
15708  {
15709  db_make_timestamp (result, result_utime);
15710  }
15711  }
15712  break;
15713 
15714  case DB_TYPE_TIMESTAMPTZ:
15715  {
15716  DB_TIMESTAMPTZ *ts_tz_p, ts_tz_res, ts_tz_fixed;
15717  DB_UTIME utime, result_utime;
15718  DB_VALUE *other;
15719  DB_BIGINT bi;
15720 
15721  if (DB_VALUE_TYPE (arg1) == DB_TYPE_TIMESTAMPTZ)
15722  {
15723  ts_tz_p = db_get_timestamptz (arg1);
15724  other = arg2;
15725  }
15726  else
15727  {
15728  ts_tz_p = db_get_timestamptz (arg2);
15729  other = arg1;
15730  }
15731 
15732  utime = ts_tz_p->timestamp;
15733 
15734  if (utime == 0)
15735  {
15736  /* operation with zero date returns null */
15737  db_make_null (result);
15739  {
15741  goto error_zerodate;
15742  }
15743  break;
15744  }
15745 
15746  switch (DB_VALUE_TYPE (other))
15747  {
15748  case DB_TYPE_INTEGER:
15749  bi = db_get_int (other);
15750  break;
15751  case DB_TYPE_SMALLINT:
15752  bi = db_get_short (other);
15753  break;
15754  case DB_TYPE_BIGINT:
15755  bi = db_get_bigint (other);
15756  break;
15757  default:
15758  return 0;
15759  }
15760 
15761  if (bi < 0)
15762  {
15763  if (bi == DB_BIGINT_MIN)
15764  {
15765  if (utime == 0)
15766  {
15767  goto overflow;
15768  }
15769  else
15770  {
15771  bi++;
15772  utime--;
15773  }
15774  }
15775  if (OR_CHECK_UNS_SUB_UNDERFLOW (utime, -bi, utime + bi))
15776  {
15777  goto overflow;
15778  }
15779  result_utime = (DB_UTIME) (utime + bi);
15780  }
15781  else
15782  {
15783  result_utime = (DB_UTIME) (utime + bi);
15784  if (OR_CHECK_UNS_ADD_OVERFLOW (utime, bi, result_utime) || INT_MAX < result_utime)
15785  {
15786  goto overflow;
15787  }
15788  }
15789 
15790  ts_tz_res.timestamp = result_utime;
15791  ts_tz_res.tz_id = ts_tz_p->tz_id;
15792 
15793  if (tz_timestamptz_fix_zone (&ts_tz_res, &ts_tz_fixed) != NO_ERROR)
15794  {
15795  return 0;
15796  }
15797 
15798  db_make_timestamptz (result, &ts_tz_fixed);
15799  }
15800  break;
15801 
15802  case DB_TYPE_DATETIME:
15803  case DB_TYPE_DATETIMELTZ:
15804  {
15805  DB_DATETIME *datetime, result_datetime;
15806  DB_BIGINT bi1, bi2, result_bi, tmp_bi;
15807  DB_VALUE *other;
15808 
15810  {
15811  datetime = db_get_datetime (arg1);
15812  other = arg2;
15813  }
15814  else
15815  {
15816  datetime = db_get_datetime (arg2);
15817  other = arg1;
15818  }
15819 
15820  if (datetime->date == 0 && datetime->time == 0)
15821  {
15822  /* operation with zero date returns null */
15823  db_make_null (result);
15825  {
15827  goto error_zerodate;
15828  }
15829  break;
15830  }
15831 
15832  switch (DB_VALUE_TYPE (other))
15833  {
15834  case DB_TYPE_SMALLINT:
15835  bi2 = (DB_BIGINT) db_get_short (other);
15836  break;
15837  case DB_TYPE_INTEGER:
15838  bi2 = (DB_BIGINT) db_get_int (other);
15839  break;
15840  default:
15841  bi2 = (DB_BIGINT) db_get_bigint (other);
15842  break;
15843  }
15844 
15845  bi1 = ((DB_BIGINT) datetime->date) * MILLISECONDS_OF_ONE_DAY + datetime->time;
15846 
15847  if (bi2 < 0)
15848  {
15849  if (bi2 == DB_BIGINT_MIN)
15850  {
15851  goto overflow;
15852  }
15853  result_bi = bi1 + bi2;
15854  if (OR_CHECK_SUB_UNDERFLOW (bi1, bi2, result_bi))
15855  {
15856  goto overflow;
15857  }
15858  }
15859  else
15860  {
15861  result_bi = bi1 + bi2;
15862  if (OR_CHECK_ADD_OVERFLOW (bi1, bi2, result_bi))
15863  {
15864  goto overflow;
15865  }
15866  }
15867 
15868  tmp_bi = (DB_BIGINT) (result_bi / MILLISECONDS_OF_ONE_DAY);
15869  if (OR_CHECK_INT_OVERFLOW (tmp_bi) || tmp_bi > DB_DATE_MAX || tmp_bi < DB_DATE_MIN)
15870  {
15871  goto overflow;
15872  }
15873  result_datetime.date = (int) tmp_bi;
15874  result_datetime.time = (int) (result_bi % MILLISECONDS_OF_ONE_DAY);
15875 
15876  if (typ == DB_TYPE_DATETIME)
15877  {
15878  db_make_datetime (result, &result_datetime);
15879  }
15880  else
15881  {
15882  db_make_datetimeltz (result, &result_datetime);
15883  }
15884  }
15885  break;
15886 
15887  case DB_TYPE_DATETIMETZ:
15888  {
15889  DB_DATETIMETZ *dt_tz_p, dt_tz_res, dt_tz_fixed;
15890  DB_DATETIME datetime;
15891  DB_BIGINT bi1, bi2, result_bi, tmp_bi;
15892  DB_VALUE *other;
15893 
15894  if (DB_VALUE_TYPE (arg1) == DB_TYPE_DATETIMETZ)
15895  {
15896  dt_tz_p = db_get_datetimetz (arg1);
15897  other = arg2;
15898  }
15899  else
15900  {
15901  dt_tz_p = db_get_datetimetz (arg2);
15902  other = arg1;
15903  }
15904 
15905  datetime = dt_tz_p->datetime;
15906 
15907  if (datetime.date == 0 && datetime.time == 0)
15908  {
15909  /* operation with zero date returns null */
15910  db_make_null (result);
15912  {
15914  goto error_zerodate;
15915  }
15916  break;
15917  }
15918 
15919  switch (DB_VALUE_TYPE (other))
15920  {
15921  case DB_TYPE_SMALLINT:
15922  bi2 = (DB_BIGINT) db_get_short (other);
15923  break;
15924  case DB_TYPE_INTEGER:
15925  bi2 = (DB_BIGINT) db_get_int (other);
15926  break;
15927  default:
15928  bi2 = (DB_BIGINT) db_get_bigint (other);
15929  break;
15930  }
15931 
15932  bi1 = ((DB_BIGINT) datetime.date) * MILLISECONDS_OF_ONE_DAY + datetime.time;
15933 
15934  if (bi2 < 0)
15935  {
15936  if (bi2 == DB_BIGINT_MIN)
15937  {
15938  goto overflow;
15939  }
15940  result_bi = bi1 + bi2;
15941  if (OR_CHECK_SUB_UNDERFLOW (bi1, bi2, result_bi))
15942  {
15943  goto overflow;
15944  }
15945  }
15946  else
15947  {
15948  result_bi = bi1 + bi2;
15949  if (OR_CHECK_ADD_OVERFLOW (bi1, bi2, result_bi))
15950  {
15951  goto overflow;
15952  }
15953  }
15954 
15955  tmp_bi = (DB_BIGINT) (result_bi / MILLISECONDS_OF_ONE_DAY);
15956  if (OR_CHECK_INT_OVERFLOW (tmp_bi) || tmp_bi > DB_DATE_MAX || tmp_bi < DB_DATE_MIN)
15957  {
15958  goto overflow;
15959  }
15960  dt_tz_res.datetime.date = (int) tmp_bi;
15961  dt_tz_res.datetime.time = (int) (result_bi % MILLISECONDS_OF_ONE_DAY);
15962  dt_tz_res.tz_id = dt_tz_p->tz_id;
15963 
15964  if (tz_datetimetz_fix_zone (&dt_tz_res, &dt_tz_fixed) != NO_ERROR)
15965  {
15966  return 0;
15967  }
15968 
15969  db_make_datetimetz (result, &dt_tz_fixed);
15970  }
15971  break;
15972 
15973  case DB_TYPE_DATE:
15974  {
15975  DB_DATE *date, result_date;
15976  DB_VALUE *other;
15977  DB_BIGINT bi;
15978 
15979  if (DB_VALUE_TYPE (arg1) == DB_TYPE_DATE)
15980  {
15981  date = db_get_date (arg1);
15982  other = arg2;
15983  }
15984  else
15985  {
15986  date = db_get_date (arg2);
15987  other = arg1;
15988  }
15989 
15990  if (*date == 0)
15991  {
15992  /* operation with zero date returns null */
15993  db_make_null (result);
15995  {
15997  goto error_zerodate;
15998  }
15999  break;
16000  }
16001 
16002  switch (DB_VALUE_TYPE (other))
16003  {
16004  case DB_TYPE_INTEGER:
16005  bi = db_get_int (other);
16006  break;
16007  case DB_TYPE_SMALLINT:
16008  bi = db_get_short (other);
16009  break;
16010  case DB_TYPE_BIGINT:
16011  bi = db_get_bigint (other);
16012  break;
16013  default:
16014  return 0;
16015  }
16016  if (bi < 0)
16017  {
16018  if (bi == DB_BIGINT_MIN)
16019  {
16020  if (*date == 0)
16021  {
16022  goto overflow;
16023  }
16024  bi++;
16025  (*date)--;
16026  }
16027  if (OR_CHECK_UNS_SUB_UNDERFLOW (*date, -bi, (*date) + bi) || (*date) + bi < DB_DATE_MIN)
16028  {
16029  goto overflow;
16030  }
16031  result_date = (DB_DATE) ((*date) + bi);
16032  }
16033  else
16034  {
16035  result_date = (DB_DATE) (*date + bi);
16036  if (OR_CHECK_UNS_ADD_OVERFLOW (*date, bi, result_date) || result_date > DB_DATE_MAX)
16037  {
16038  goto overflow;
16039  }
16040  }
16041 
16042  db_value_put_encoded_date (result, &result_date);
16043  }
16044  break;
16045  default:
16046  return 0;
16047  }
16048  break;
16049 
16050  case PT_MINUS:
16051  switch (typ)
16052  {
16053  case DB_TYPE_SET:
16054  case DB_TYPE_MULTISET:
16055  if (!pt_difference_sets (parser, domain, arg1, arg2, result, o2))
16056  {
16057  return 0; /* set union failed */
16058  }
16059  break;
16060 
16061  case DB_TYPE_INTEGER:
16062  {
16063  int i1, i2, itmp;
16064 
16065  i1 = db_get_int (arg1);
16066  i2 = db_get_int (arg2);
16067  itmp = i1 - i2;
16068  if (OR_CHECK_SUB_UNDERFLOW (i1, i2, itmp))
16069  goto overflow;
16070  else
16071  db_make_int (result, itmp);
16072  break;
16073  }
16074 
16075  case DB_TYPE_BIGINT:
16076  {
16077  DB_BIGINT bi1, bi2, result_bi;
16078 
16079  bi1 = bi2 = 0;
16080  if (typ1 != typ2)
16081  {
16082  assert (false);
16083 
16084  db_make_null (result);
16085  break;
16086  }
16087 
16088  if (typ1 == DB_TYPE_DATETIME || typ1 == DB_TYPE_DATETIMELTZ)
16089  {
16090  DB_DATETIME *dt1, *dt2;
16091 
16092  dt1 = db_get_datetime (arg1);
16093  dt2 = db_get_datetime (arg2);
16094 
16095  bi1 = (((DB_BIGINT) dt1->date) * MILLISECONDS_OF_ONE_DAY + dt1->time);
16096  bi2 = (((DB_BIGINT) dt2->date) * MILLISECONDS_OF_ONE_DAY + dt2->time);
16097  }
16098  else if (typ1 == DB_TYPE_DATETIMETZ)
16099  {
16100  DB_DATETIMETZ *dt_tz1, *dt_tz2;
16101 
16102  dt_tz1 = db_get_datetimetz (arg1);
16103  dt_tz2 = db_get_datetimetz (arg2);
16104 
16105  bi1 = (((DB_BIGINT) dt_tz1->datetime.date) * MILLISECONDS_OF_ONE_DAY + dt_tz1->datetime.time);
16106  bi2 = (((DB_BIGINT) dt_tz2->datetime.date) * MILLISECONDS_OF_ONE_DAY + dt_tz2->datetime.time);
16107  }
16108  else if (typ1 == DB_TYPE_DATE)
16109  {
16110  DB_DATE *d1, *d2;
16111 
16112  d1 = db_get_date (arg1);
16113  d2 = db_get_date (arg2);
16114 
16115  bi1 = (DB_BIGINT) (*d1);
16116  bi2 = (DB_BIGINT) (*d2);
16117  }
16118  else if (typ1 == DB_TYPE_TIME)
16119  {
16120  DB_TIME *t1, *t2;
16121 
16122  t1 = db_get_time (arg1);
16123  t2 = db_get_time (arg2);
16124 
16125  bi1 = (DB_BIGINT) (*t1);
16126  bi2 = (DB_BIGINT) (*t2);
16127  }
16128  else if (typ1 == DB_TYPE_TIMESTAMP || typ1 == DB_TYPE_TIMESTAMPLTZ)
16129  {
16130  DB_TIMESTAMP *ts1, *ts2;
16131 
16132  ts1 = db_get_timestamp (arg1);
16133  ts2 = db_get_timestamp (arg2);
16134 
16135  bi1 = (DB_BIGINT) (*ts1);
16136  bi2 = (DB_BIGINT) (*ts2);
16137  }
16138  else if (typ1 == DB_TYPE_TIMESTAMPTZ)
16139  {
16140  DB_TIMESTAMPTZ *ts_tz1, *ts_tz2;
16141 
16142  ts_tz1 = db_get_timestamptz (arg1);
16143  ts_tz2 = db_get_timestamptz (arg2);
16144 
16145  bi1 = (DB_BIGINT) (ts_tz1->timestamp);
16146  bi2 = (DB_BIGINT) (ts_tz2->timestamp);
16147  }
16148  else if (typ1 == DB_TYPE_BIGINT)
16149  {
16150  bi1 = db_get_bigint (arg1);
16151  bi2 = db_get_bigint (arg2);
16152  }
16153  else
16154  {
16155  assert (false);
16156 
16157  db_make_null (result);
16158  break;
16159  }
16160 
16161  if ((TP_IS_DATE_TYPE (typ1) && bi1 == 0) || (TP_IS_DATE_TYPE (typ2) && bi2 == 0))
16162  {
16163  /* operation with zero date returns null */
16164  db_make_null (result);
16166  {
16168  goto error_zerodate;
16169  }
16170  break;
16171  }
16172 
16173  result_bi = bi1 - bi2;
16174  if (OR_CHECK_SUB_UNDERFLOW (bi1, bi2, result_bi))
16175  {
16176  goto overflow;
16177  }
16178  else
16179  {
16180  db_make_bigint (result, result_bi);
16181  }
16182  break;
16183  }
16184 
16185  case DB_TYPE_SHORT:
16186  {
16187  short s1, s2, stmp;
16188 
16189  s1 = db_get_short (arg1);
16190  s2 = db_get_short (arg2);
16191  stmp = s1 - s2;
16192  if (OR_CHECK_SUB_UNDERFLOW (s1, s2, stmp))
16193  goto overflow;
16194  else
16195  db_make_short (result, stmp);
16196  break;
16197  }
16198 
16199  case DB_TYPE_FLOAT:
16200  {
16201  float ftmp;
16202 
16203  ftmp = db_get_float (arg1) - db_get_float (arg2);
16204  if (OR_CHECK_FLOAT_OVERFLOW (ftmp))
16205  goto overflow;
16206  else
16207  db_make_float (result, ftmp);
16208  break;
16209  }
16210 
16211  case DB_TYPE_DOUBLE:
16212  {
16213  double dtmp;
16214 
16215  dtmp = db_get_double (arg1) - db_get_double (arg2);
16216  if (OR_CHECK_DOUBLE_OVERFLOW (dtmp))
16217  goto overflow;
16218  else
16219  db_make_double (result, dtmp);
16220  break;
16221  }
16222 
16223  case DB_TYPE_NUMERIC:
16224  if (numeric_db_value_sub (arg1, arg2, result) != NO_ERROR)
16225  {
16226  PT_ERRORc (parser, o1, er_msg ());
16227  return 0;
16228  }
16229  dom_status = tp_value_coerce (result, result, domain);
16230  if (dom_status != DOMAIN_COMPATIBLE)
16231  {
16232  (void) tp_domain_status_er_set (dom_status, ARG_FILE_LINE, result, domain);
16233  return 0;
16234  }
16235  break;
16236 
16237  case DB_TYPE_MONETARY:
16238  {
16239  double dtmp;
16240 
16241  dtmp = db_get_monetary (arg1)->amount - db_get_monetary (arg2)->amount;
16242  if (OR_CHECK_DOUBLE_OVERFLOW (dtmp))
16243  goto overflow;
16244  else
16245  db_make_monetary (result, DB_CURRENCY_DEFAULT, dtmp);
16246  break;
16247  }
16248 
16249  case DB_TYPE_TIME:
16250  {
16251  DB_TIME time, result_time;
16252  int hour, minute, second;
16253  DB_BIGINT bi = 0, ubi = 0;
16254 
16255  switch (DB_VALUE_TYPE (arg2))
16256  {
16257  case DB_TYPE_SHORT:
16258  bi = db_get_short (arg2);
16259  break;
16260  case DB_TYPE_INTEGER:
16261  bi = db_get_int (arg2);
16262  break;
16263  case DB_TYPE_BIGINT:
16264  bi = db_get_bigint (arg2);
16265  break;
16266  default:
16267  assert (false);
16268  break;
16269  }
16270  ubi = (bi < 0) ? -bi : bi;
16271 
16272  assert (typ == DB_VALUE_TYPE (arg1));
16273 
16274  time = *db_get_time (arg1);
16275 
16276  if (time < (DB_TIME) (ubi % 86400))
16277  {
16278  time += 86400;
16279  }
16280  result_time = time - (bi % 86400);
16281 
16282  db_time_decode (&result_time, &hour, &minute, &second);
16283  db_make_time (result, hour, minute, second);
16284  }
16285  break;
16286 
16287  case DB_TYPE_TIMESTAMP:
16288  case DB_TYPE_TIMESTAMPLTZ:
16289  case DB_TYPE_TIMESTAMPTZ:
16290  {
16291  DB_UTIME utime, result_utime;
16292  DB_TIMESTAMPTZ ts_tz, ts_tz_fixed;
16293  DB_BIGINT bi = 0;
16294 
16295  switch (DB_VALUE_TYPE (arg2))
16296  {
16297  case DB_TYPE_SHORT:
16298  bi = db_get_short (arg2);
16299  break;
16300  case DB_TYPE_INTEGER:
16301  bi = db_get_int (arg2);
16302  break;
16303  case DB_TYPE_BIGINT:
16304  bi = db_get_bigint (arg2);
16305  break;
16306  default:
16307  assert (false);
16308  break;
16309  }
16310 
16311  assert (typ == DB_VALUE_TYPE (arg1));
16312  if (typ == DB_TYPE_TIMESTAMPTZ)
16313  {
16314  ts_tz = *db_get_timestamptz (arg1);
16315  utime = ts_tz.timestamp;
16316  }
16317  else
16318  {
16319  utime = *db_get_timestamp (arg1);
16320  }
16321 
16322  if (utime == 0)
16323  {
16324  /* operation with zero date returns null */
16325  db_make_null (result);
16327  {
16329  goto error_zerodate;
16330  }
16331  break;
16332  }
16333 
16334  if (bi < 0)
16335  {
16336  /* we're adding */
16337  result_utime = (DB_UTIME) (utime - bi);
16338  if (OR_CHECK_UNS_ADD_OVERFLOW (utime, -bi, result_utime) || INT_MAX < result_utime)
16339  {
16340  goto overflow;
16341  }
16342  }
16343  else
16344  {
16345  result_utime = (DB_UTIME) (utime - bi);
16346  if (OR_CHECK_UNS_SUB_UNDERFLOW (utime, bi, result_utime))
16347  {
16349  return 0;
16350  }
16351  }
16352  if (typ == DB_TYPE_TIMESTAMPTZ)
16353  {
16354  ts_tz.timestamp = result_utime;
16355  if (tz_timestamptz_fix_zone (&ts_tz, &ts_tz_fixed) != NO_ERROR)
16356  {
16357  return 0;
16358  }
16359  db_make_timestamptz (result, &ts_tz_fixed);
16360  }
16361  else if (typ == DB_TYPE_TIMESTAMPLTZ)
16362  {
16363  db_make_timestampltz (result, result_utime);
16364  }
16365  else
16366  {
16367  db_make_timestamp (result, result_utime);
16368  }
16369  }
16370  break;
16371 
16372  case DB_TYPE_DATETIME:
16373  case DB_TYPE_DATETIMELTZ:
16374  case DB_TYPE_DATETIMETZ:
16375  {
16376  DB_DATETIME datetime, result_datetime;
16377  DB_DATETIMETZ dt_tz, dt_tz_fixed;
16378  DB_BIGINT bi = 0;
16379 
16380  switch (DB_VALUE_TYPE (arg2))
16381  {
16382  case DB_TYPE_SHORT:
16383  bi = db_get_short (arg2);
16384  break;
16385  case DB_TYPE_INTEGER:
16386  bi = db_get_int (arg2);
16387  break;
16388  case DB_TYPE_BIGINT:
16389  bi = db_get_bigint (arg2);
16390  break;
16391  default:
16392  assert (false);
16393  break;
16394  }
16395 
16396  if (typ == DB_TYPE_DATETIMETZ)
16397  {
16398  dt_tz = *db_get_datetimetz (arg1);
16399  datetime = dt_tz.datetime;
16400  }
16401  else
16402  {
16403  datetime = *db_get_datetime (arg1);
16404  }
16405 
16406  if (datetime.date == 0 && datetime.time == 0)
16407  {
16408  /* operation with zero date returns null */
16409  db_make_null (result);
16411  {
16413  goto error_zerodate;
16414  }
16415  break;
16416  }
16417 
16418  error = db_subtract_int_from_datetime (&datetime, bi, &result_datetime);
16419  if (error != NO_ERROR)
16420  {
16422  return 0;
16423  }
16424 
16425  if (typ == DB_TYPE_DATETIMETZ)
16426  {
16427  dt_tz.datetime = result_datetime;
16428  if (tz_datetimetz_fix_zone (&dt_tz, &dt_tz_fixed) != NO_ERROR)
16429  {
16430  return 0;
16431  }
16432  db_make_datetimetz (result, &dt_tz_fixed);
16433  }
16434  else if (typ == DB_TYPE_DATETIMELTZ)
16435  {
16436  db_make_datetimeltz (result, &result_datetime);
16437  }
16438  else
16439  {
16440  db_make_datetime (result, &result_datetime);
16441  }
16442  }
16443  break;
16444 
16445  case DB_TYPE_DATE:
16446  {
16447  DB_DATE *date, result_date;
16448  int month, day, year;
16449  DB_BIGINT bi = 0;
16450 
16451  switch (DB_VALUE_TYPE (arg2))
16452  {
16453  case DB_TYPE_SHORT:
16454  bi = db_get_short (arg2);
16455  break;
16456  case DB_TYPE_INTEGER:
16457  bi = db_get_int (arg2);
16458  break;
16459  case DB_TYPE_BIGINT:
16460  bi = db_get_bigint (arg2);
16461  break;
16462  default:
16463  assert (false);
16464  break;
16465  }
16466  date = db_get_date (arg1);
16467 
16468  if (*date == 0)
16469  {
16470  /* operation with zero date returns null */
16471  db_make_null (result);
16473  {
16475  goto error_zerodate;
16476  }
16477  break;
16478  }
16479 
16480  if (bi < 0)
16481  {
16482  /* we're adding */
16483  result_date = (DB_DATE) (*date - bi);
16484  if (OR_CHECK_UNS_ADD_OVERFLOW (*date, -bi, result_date) || result_date > DB_DATE_MAX)
16485  {
16486  goto overflow;
16487  }
16488  }
16489  else
16490  {
16491  result_date = *date - (DB_DATE) bi;
16492  if (OR_CHECK_UNS_SUB_UNDERFLOW (*date, bi, result_date) || result_date < DB_DATE_MIN)
16493  {
16495  return 0;
16496  }
16497  }
16498  db_date_decode (&result_date, &month, &day, &year);
16499  db_make_date (result, month, day, year);
16500  }
16501  break;
16502 
16503  default:
16504  return 0;
16505  }
16506  break;
16507 
16508  case PT_TIMES:
16509  switch (typ)
16510  {
16511  case DB_TYPE_SET:
16512  case DB_TYPE_MULTISET:
16513  case DB_TYPE_SEQUENCE:
16514  if (!pt_product_sets (parser, domain, arg1, arg2, result, o2))
16515  {
16516  return 0; /* set union failed */
16517  }
16518  break;
16519 
16520  case DB_TYPE_INTEGER:
16521  {
16522  /* NOTE that we need volatile to prevent optimizer from generating division expression as
16523  * multiplication.
16524  */
16525  volatile int i1, i2, itmp;
16526 
16527  i1 = db_get_int (arg1);
16528  i2 = db_get_int (arg2);
16529  itmp = i1 * i2;
16530  if (OR_CHECK_MULT_OVERFLOW (i1, i2, itmp))
16531  {
16532  goto overflow;
16533  }
16534  else
16535  {
16536  db_make_int (result, itmp);
16537  }
16538  break;
16539  }
16540 
16541  case DB_TYPE_BIGINT:
16542  {
16543  /* NOTE that we need volatile to prevent optimizer from generating division expression as
16544  * multiplication.
16545  */
16546  volatile DB_BIGINT bi1, bi2, bitmp;
16547 
16548  bi1 = db_get_bigint (arg1);
16549  bi2 = db_get_bigint (arg2);
16550  bitmp = bi1 * bi2;
16551  if (OR_CHECK_MULT_OVERFLOW (bi1, bi2, bitmp))
16552  {
16553  goto overflow;
16554  }
16555  else
16556  {
16557  db_make_bigint (result, bitmp);
16558  }
16559  break;
16560  }
16561 
16562  case DB_TYPE_SHORT:
16563  {
16564  /* NOTE that we need volatile to prevent optimizer from generating division expression as
16565  * multiplication.
16566  */
16567  volatile short s1, s2, stmp;
16568 
16569  s1 = db_get_short (arg1);
16570  s2 = db_get_short (arg2);
16571  stmp = s1 * s2;
16572  if (OR_CHECK_MULT_OVERFLOW (s1, s2, stmp))
16573  {
16574  goto overflow;
16575  }
16576  else
16577  {
16578  db_make_short (result, stmp);
16579  }
16580  break;
16581  }
16582 
16583  case DB_TYPE_FLOAT:
16584  {
16585  float ftmp;
16586 
16587  ftmp = db_get_float (arg1) * db_get_float (arg2);
16588  if (OR_CHECK_FLOAT_OVERFLOW (ftmp))
16589  {
16590  goto overflow;
16591  }
16592  else
16593  {
16594  db_make_float (result, ftmp);
16595  }
16596  break;
16597  }
16598 
16599  case DB_TYPE_DOUBLE:
16600  {
16601  double dtmp;
16602 
16603  dtmp = db_get_double (arg1) * db_get_double (arg2);
16604  if (OR_CHECK_DOUBLE_OVERFLOW (dtmp))
16605  {
16606  goto overflow;
16607  }
16608  else
16609  {
16610  db_make_double (result, dtmp);
16611  }
16612  break;
16613  }
16614 
16615  case DB_TYPE_NUMERIC:
16616  error = numeric_db_value_mul (arg1, arg2, result);
16617  if (error == ER_IT_DATA_OVERFLOW)
16618  {
16619  goto overflow;
16620  }
16621  else if (error != NO_ERROR)
16622  {
16623  PT_ERRORc (parser, o1, er_msg ());
16624  return 0;
16625  }
16626  dom_status = tp_value_coerce (result, result, domain);
16627  if (dom_status != DOMAIN_COMPATIBLE)
16628  {
16629  (void) tp_domain_status_er_set (dom_status, ARG_FILE_LINE, result, domain);
16630  return 0;
16631  }
16632  break;
16633 
16634  case DB_TYPE_MONETARY:
16635  {
16636  double dtmp;
16637 
16638  dtmp = db_get_monetary (arg1)->amount * db_get_monetary (arg2)->amount;
16639  if (OR_CHECK_DOUBLE_OVERFLOW (dtmp))
16640  {
16641  goto overflow;
16642  }
16643  else
16644  {
16645  db_make_monetary (result, DB_CURRENCY_DEFAULT, dtmp);
16646  }
16647  break;
16648  }
16649  break;
16650 
16651  default:
16652  return 0;
16653  }
16654  break;
16655 
16656  case PT_DIVIDE:
16657  switch (typ)
16658  {
16659  case DB_TYPE_SHORT:
16660  if (db_get_short (arg2) != 0)
16661  {
16662  db_make_short (result, db_get_short (arg1) / db_get_short (arg2));
16663  return 1;
16664  }
16665  break;
16666 
16667  case DB_TYPE_INTEGER:
16668  if (db_get_int (arg2) != 0)
16669  {
16670  db_make_int (result, (db_get_int (arg1) / db_get_int (arg2)));
16671  return 1;
16672  }
16673  break;
16674  case DB_TYPE_BIGINT:
16675  if (db_get_bigint (arg2) != 0)
16676  {
16677  db_make_bigint (result, (db_get_bigint (arg1) / db_get_bigint (arg2)));
16678  return 1;
16679  }
16680  break;
16681  case DB_TYPE_FLOAT:
16682  if (fabs (db_get_float (arg2)) > FLT_EPSILON)
16683  {
16684  float ftmp;
16685 
16686  ftmp = db_get_float (arg1) / db_get_float (arg2);
16687  if (OR_CHECK_FLOAT_OVERFLOW (ftmp))
16688  {
16689  goto overflow;
16690  }
16691  else
16692  {
16693  db_make_float (result, ftmp);
16694  return 1;
16695  }
16696  }
16697  break;
16698 
16699  case DB_TYPE_DOUBLE:
16700  if (fabs (db_get_double (arg2)) > DBL_EPSILON)
16701  {
16702  double dtmp;
16703 
16704  dtmp = db_get_double (arg1) / db_get_double (arg2);
16705  if (OR_CHECK_DOUBLE_OVERFLOW (dtmp))
16706  {
16707  goto overflow;
16708  }
16709  else
16710  {
16711  db_make_double (result, dtmp);
16712  return 1; /* success */
16713  }
16714  }
16715  break;
16716 
16717  case DB_TYPE_NUMERIC:
16718  if (!numeric_db_value_is_zero (arg2))
16719  {
16720  error = numeric_db_value_div (arg1, arg2, result);
16721  if (error == ER_IT_DATA_OVERFLOW)
16722  {
16723  goto overflow;
16724  }
16725  else if (error != NO_ERROR)
16726  {
16727  PT_ERRORc (parser, o1, er_msg ());
16728  return 0;
16729  }
16730 
16731  dom_status = tp_value_coerce (result, result, domain);
16732  if (dom_status != DOMAIN_COMPATIBLE)
16733  {
16734  (void) tp_domain_status_er_set (dom_status, ARG_FILE_LINE, result, domain);
16735  return 0;
16736  }
16737 
16738  return 1;
16739  }
16740  break;
16741 
16742  case DB_TYPE_MONETARY:
16743  if (fabs (db_get_monetary (arg2)->amount) > DBL_EPSILON)
16744  {
16745  double dtmp;
16746 
16747  dtmp = db_get_monetary (arg1)->amount / db_get_monetary (arg2)->amount;
16748  if (OR_CHECK_DOUBLE_OVERFLOW (dtmp))
16749  {
16750  goto overflow;
16751  }
16752  else
16753  {
16754  db_make_monetary (result, DB_CURRENCY_DEFAULT, dtmp);
16755  return 1; /* success */
16756  }
16757  }
16758  break;
16759 
16760  default:
16761  return 0;
16762  }
16763 
16765  return 0;
16766 
16767  default:
16768  return 0;
16769  }
16770  break;
16771 
16772  case PT_STRCAT:
16773  if (typ1 == DB_TYPE_NULL || typ2 == DB_TYPE_NULL)
16774  {
16775  bool check_empty_string;
16776 
16777  check_empty_string = ((prm_get_bool_value (PRM_ID_ORACLE_STYLE_EMPTY_STRING)) ? true : false);
16778 
16779  if (!check_empty_string || !PT_IS_STRING_TYPE (rTyp))
16780  {
16781  db_make_null (result); /* NULL arith_op any = NULL */
16782  break;
16783  }
16784  }
16785 
16786  /* screen out cases we don't evaluate */
16787  if (!PT_IS_STRING_TYPE (rTyp))
16788  {
16789  return 0;
16790  }
16791 
16792  switch (typ)
16793  {
16794  case DB_TYPE_CHAR:
16795  case DB_TYPE_NCHAR:
16796  case DB_TYPE_VARCHAR:
16797  case DB_TYPE_VARNCHAR:
16798  case DB_TYPE_BIT:
16799  case DB_TYPE_VARBIT:
16800  if (db_string_concatenate (arg1, arg2, result, &truncation) < 0 || truncation != DATA_STATUS_OK)
16801  {
16802  PT_ERRORc (parser, o1, er_msg ());
16803  return 0;
16804  }
16805  break;
16806 
16807  default:
16808  return 0;
16809  }
16810  break;
16811 
16812  case PT_MODULUS:
16813  error = db_mod_dbval (result, arg1, arg2);
16814  if (error != NO_ERROR)
16815  {
16816  PT_ERRORc (parser, o1, er_msg ());
16817  return 0;
16818  }
16819  break;
16820 
16821  case PT_PI:
16822  db_make_double (result, 3.14159265358979323846264338);
16823  break;
16824 
16825  case PT_RAND:
16826  /* rand() and drand() should always generate the same value during a statement. To support it, we add lrand and
16827  * drand member to PARSER_CONTEXT. */
16828  if (DB_IS_NULL (arg1))
16829  {
16830  db_make_int (result, parser->lrand);
16831  }
16832  else
16833  {
16834  srand48 (db_get_int (arg1));
16835  db_make_int (result, lrand48 ());
16836  }
16837  break;
16838 
16839  case PT_DRAND:
16840  if (DB_IS_NULL (arg1))
16841  {
16842  db_make_double (result, parser->drand);
16843  }
16844  else
16845  {
16846  srand48 (db_get_int (arg1));
16847  db_make_double (result, drand48 ());
16848  }
16849  break;
16850 
16851  case PT_RANDOM:
16852  /* Generate seed internally if there is no seed given as argument. rand() on select list gets a random value by
16853  * fetch_peek_arith(). But, if rand() is specified on VALUES clause of insert statement, it gets a random value
16854  * by the following codes. In this case, DB_VALUE(arg1) of NULL type is passed. */
16855  if (DB_IS_NULL (arg1))
16856  {
16857  struct timeval t;
16858  gettimeofday (&t, NULL);
16859  srand48 ((long) (t.tv_usec + lrand48 ()));
16860  }
16861  else
16862  {
16863  srand48 (db_get_int (arg1));
16864  }
16865  db_make_int (result, lrand48 ());
16866  break;
16867 
16868  case PT_DRANDOM:
16869  if (DB_IS_NULL (arg1))
16870  {
16871  struct timeval t;
16872  gettimeofday (&t, NULL);
16873  srand48 ((long) (t.tv_usec + lrand48 ()));
16874  }
16875  else
16876  {
16877  srand48 (db_get_int (arg1));
16878  }
16879  db_make_double (result, drand48 ());
16880  break;
16881 
16882  case PT_FLOOR:
16883  error = db_floor_dbval (result, arg1);
16884  if (error != NO_ERROR)
16885  {
16886  PT_ERRORc (parser, o1, er_msg ());
16887  return 0;
16888  }
16889  break;
16890 
16891  case PT_CEIL:
16892  error = db_ceil_dbval (result, arg1);
16893  if (error != NO_ERROR)
16894  {
16895  PT_ERRORc (parser, o1, er_msg ());
16896  return 0;
16897  }
16898  break;
16899 
16900  case PT_SIGN:
16901  error = db_sign_dbval (result, arg1);
16902  if (error != NO_ERROR)
16903  {
16904  PT_ERRORc (parser, o1, er_msg ());
16905  return 0;
16906  }
16907  break;
16908 
16909  case PT_ABS:
16910  error = db_abs_dbval (result, arg1);
16911  if (error != NO_ERROR)
16912  {
16913  PT_ERRORc (parser, o1, er_msg ());
16914  return 0;
16915  }
16916  break;
16917  case PT_JSON_CONTAINS:
16918  error = db_json_contains_dbval (arg1, arg2, (o3 == NULL ? NULL : arg3), result);
16919  if (error != NO_ERROR)
16920  {
16921  PT_ERRORc (parser, o1, er_msg ());
16922  return 0;
16923  }
16924  break;
16925  case PT_JSON_TYPE:
16926  error = db_json_type_dbval (arg1, result);
16927  if (error != NO_ERROR)
16928  {
16929  PT_ERRORc (parser, o1, er_msg ());
16930  return 0;
16931  }
16932  break;
16933  case PT_JSON_EXTRACT:
16934  error = db_json_extract_dbval (arg1, arg2, result);
16935  if (error != NO_ERROR)
16936  {
16937  PT_ERRORc (parser, o1, er_msg ());
16938  return 0;
16939  }
16940  break;
16941  case PT_JSON_VALID:
16942  error = db_json_valid_dbval (arg1, result);
16943  if (error != NO_ERROR)
16944  {
16945  PT_ERRORc (parser, o1, er_msg ());
16946  return 0;
16947  }
16948  break;
16949  case PT_JSON_LENGTH:
16950  error = db_json_length_dbval (arg1, (o2 == NULL ? NULL : arg2), result);
16951  if (error != NO_ERROR)
16952  {
16953  PT_ERRORc (parser, o1, er_msg ());
16954  return 0;
16955  }
16956  break;
16957  case PT_JSON_DEPTH:
16958  error = db_json_depth_dbval (arg1, result);
16959  if (error != NO_ERROR)
16960  {
16961  PT_ERRORc (parser, o1, er_msg ());
16962  return 0;
16963  }
16964  break;
16965  case PT_JSON_SEARCH:
16968  PT_ERRORc (parser, o1, er_msg ());
16969  return 0;
16970  case PT_JSON_PRETTY:
16971  error = db_json_pretty_dbval (arg1, result);
16972  if (error != NO_ERROR)
16973  {
16974  PT_ERRORc (parser, o1, er_msg ());
16975  return 0;
16976  }
16977  break;
16978  case PT_POWER:
16979  error = db_power_dbval (result, arg1, arg2);
16980  if (error != NO_ERROR)
16981  {
16982  PT_ERRORc (parser, o1, er_msg ());
16983  return 0;
16984  }
16985  break;
16986 
16987  case PT_ROUND:
16988  error = db_round_dbval (result, arg1, arg2);
16989  if (error != NO_ERROR)
16990  {
16991  PT_ERRORc (parser, o1, er_msg ());
16992  return 0;
16993  }
16994  break;
16995 
16996  case PT_LOG:
16997  error = db_log_dbval (result, arg1, arg2);
16998  if (error != NO_ERROR)
16999  {
17000  PT_ERRORc (parser, o1, er_msg ());
17001  return 0;
17002  }
17003  break;
17004 
17005  case PT_EXP:
17006  error = db_exp_dbval (result, arg1);
17007  if (error != NO_ERROR)
17008  {
17009  PT_ERRORc (parser, o1, er_msg ());
17010  return 0;
17011  }
17012  break;
17013 
17014  case PT_SQRT:
17015  error = db_sqrt_dbval (result, arg1);
17016  if (error != NO_ERROR)
17017  {
17018  PT_ERRORc (parser, o1, er_msg ());
17019  return 0;
17020  }
17021  break;
17022 
17023  case PT_SIN:
17024  error = db_sin_dbval (result, arg1);
17025  if (error != NO_ERROR)
17026  {
17027  PT_ERRORc (parser, o1, er_msg ());
17028  return 0;
17029  }
17030  break;
17031 
17032  case PT_COS:
17033  error = db_cos_dbval (result, arg1);
17034  if (error != NO_ERROR)
17035  {
17036  PT_ERRORc (parser, o1, er_msg ());
17037  return 0;
17038  }
17039  break;
17040 
17041  case PT_TAN:
17042  error = db_tan_dbval (result, arg1);
17043  if (error != NO_ERROR)
17044  {
17045  PT_ERRORc (parser, o1, er_msg ());
17046  return 0;
17047  }
17048  break;
17049 
17050  case PT_COT:
17051  error = db_cot_dbval (result, arg1);
17052  if (error != NO_ERROR)
17053  {
17054  PT_ERRORc (parser, o1, er_msg ());
17055  return 0;
17056  }
17057  break;
17058 
17059  case PT_ACOS:
17060  error = db_acos_dbval (result, arg1);
17061  if (error != NO_ERROR)
17062  {
17063  PT_ERRORc (parser, o1, er_msg ());
17064  return 0;
17065  }
17066  break;
17067 
17068  case PT_ASIN:
17069  error = db_asin_dbval (result, arg1);
17070  if (error != NO_ERROR)
17071  {
17072  PT_ERRORc (parser, o1, er_msg ());
17073  return 0;
17074  }
17075  break;
17076 
17077  case PT_ATAN:
17078  error = db_atan_dbval (result, arg1);
17079  if (error != NO_ERROR)
17080  {
17081  PT_ERRORc (parser, o1, er_msg ());
17082  return 0;
17083  }
17084  break;
17085 
17086  case PT_ATAN2:
17087  error = db_atan2_dbval (result, arg1, arg2);
17088  if (error != NO_ERROR)
17089  {
17090  PT_ERRORc (parser, o1, er_msg ());
17091  return 0;
17092  }
17093  break;
17094 
17095  case PT_DEGREES:
17096  error = db_degrees_dbval (result, arg1);
17097  if (error != NO_ERROR)
17098  {
17099  PT_ERRORc (parser, o1, er_msg ());
17100  return 0;
17101  }
17102  break;
17103 
17104  case PT_DATEF:
17105  error = db_date_dbval (result, arg1, NULL);
17106  if (error != NO_ERROR)
17107  {
17108  PT_ERRORc (parser, o1, er_msg ());
17109  return 0;
17110  }
17111  break;
17112 
17113  case PT_TIMEF:
17114  error = db_time_dbval (result, arg1, NULL);
17115  if (error != NO_ERROR)
17116  {
17117  PT_ERRORc (parser, o1, er_msg ());
17118  return 0;
17119  }
17120  break;
17121 
17122  case PT_RADIANS:
17123  error = db_radians_dbval (result, arg1);
17124  if (error != NO_ERROR)
17125  {
17126  PT_ERRORc (parser, o1, er_msg ());
17127  return 0;
17128  }
17129  break;
17130 
17131  case PT_LN:
17132  error = db_log_generic_dbval (result, arg1, -1 /* e convention */ );
17133  if (error != NO_ERROR)
17134  {
17135  PT_ERRORc (parser, o1, er_msg ());
17136  return 0;
17137  }
17138  break;
17139 
17140  case PT_LOG2:
17141  error = db_log_generic_dbval (result, arg1, 2);
17142  if (error != NO_ERROR)
17143  {
17144  PT_ERRORc (parser, o1, er_msg ());
17145  return 0;
17146  }
17147  break;
17148 
17149  case PT_LOG10:
17150  error = db_log_generic_dbval (result, arg1, 10);
17151  if (error != NO_ERROR)
17152  {
17153  PT_ERRORc (parser, o1, er_msg ());
17154  return 0;
17155  }
17156  break;
17157 
17158  case PT_TRUNC:
17159  error = db_trunc_dbval (result, arg1, arg2);
17160  if (error != NO_ERROR)
17161  {
17162  PT_ERRORc (parser, o1, er_msg ());
17163  return 0;
17164  }
17165  break;
17166 
17167  case PT_CHR:
17168  error = db_string_chr (result, arg1, arg2);
17169  if (error != NO_ERROR)
17170  {
17171  PT_ERRORc (parser, o1, er_msg ());
17172  return 0;
17173  }
17174  break;
17175 
17176  case PT_INSTR:
17177  error = db_string_instr (arg1, arg2, arg3, result);
17178  if (error < 0)
17179  {
17180  PT_ERRORc (parser, o1, er_msg ());
17181  return 0;
17182  }
17183  else
17184  {
17185  return 1;
17186  }
17187 
17188  case PT_LEAST:
17189  error = db_least_or_greatest (arg1, arg2, result, true);
17190  if (error != NO_ERROR)
17191  {
17192  ASSERT_ERROR ();
17193  PT_ERRORc (parser, o1, er_msg ());
17194  return 0;
17195  }
17196 
17197  if (tp_value_cast (result, result, domain, true) != DOMAIN_COMPATIBLE)
17198  {
17200  pt_short_print (parser, o2), pt_show_type_enum (rTyp));
17201  return 0;
17202  }
17203 
17204  return 1;
17205 
17206  case PT_GREATEST:
17207  error = db_least_or_greatest (arg1, arg2, result, false);
17208  if (error != NO_ERROR)
17209  {
17210  ASSERT_ERROR ();
17211  PT_ERRORc (parser, o1, er_msg ());
17212  return 0;
17213  }
17214 
17215  if (tp_value_cast (result, result, domain, true) != DOMAIN_COMPATIBLE)
17216  {
17218  pt_short_print (parser, o2), pt_show_type_enum (rTyp));
17219  return 0;
17220  }
17221 
17222  return 1;
17223 
17224  case PT_POSITION:
17225  error = db_string_position (arg1, arg2, result);
17226  if (error < 0)
17227  {
17228  PT_ERRORc (parser, o1, er_msg ());
17229  return 0;
17230  }
17231  else
17232  {
17233  return 1;
17234  }
17235 
17236  case PT_FINDINSET:
17237  error = db_find_string_in_in_set (arg1, arg2, result);
17238  if (error < 0)
17239  {
17240  PT_ERRORc (parser, o1, er_msg ());
17241  return 0;
17242  }
17243  else
17244  {
17245  return 1;
17246  }
17247 
17248  case PT_SUBSTRING:
17249  if (DB_IS_NULL (arg1) || DB_IS_NULL (arg2) || (o3 && DB_IS_NULL (arg3)))
17250  {
17251  db_make_null (result);
17252  return 1;
17253  }
17254 
17256  {
17257  DB_VALUE tmp_len, tmp_arg2, tmp_arg3;
17258  int pos, len;
17259 
17260  pos = db_get_int (arg2);
17261  if (pos < 0)
17262  {
17263  if (QSTR_IS_BIT (typ1))
17264  {
17265  if (db_string_bit_length (arg1, &tmp_len) != NO_ERROR)
17266  {
17267  PT_ERRORc (parser, o1, er_msg ());
17268  return 0;
17269  }
17270  }
17271  else
17272  {
17273  if (db_string_char_length (arg1, &tmp_len) != NO_ERROR)
17274  {
17275  PT_ERRORc (parser, o1, er_msg ());
17276  return 0;
17277  }
17278  }
17279  if (DB_IS_NULL (&tmp_len))
17280  {
17281  PT_ERRORc (parser, o1, er_msg ());
17282  return 0;
17283  }
17284  pos = pos + db_get_int (&tmp_len) + 1;
17285  }
17286 
17287  if (pos < 1)
17288  {
17289  db_make_int (&tmp_arg2, 1);
17290  }
17291  else
17292  {
17293  db_make_int (&tmp_arg2, pos);
17294  }
17295 
17296  if (o3)
17297  {
17298  len = db_get_int (arg3);
17299  if (len < 1)
17300  {
17301  db_make_int (&tmp_arg3, 0);
17302  }
17303  else
17304  {
17305  db_make_int (&tmp_arg3, len);
17306  }
17307  }
17308  else
17309  {
17310  db_make_null (&tmp_arg3);
17311  }
17312 
17313  error = db_string_substring (pt_misc_to_qp_misc_operand (qualifier), arg1, &tmp_arg2, &tmp_arg3, result);
17314  if (error < 0)
17315  {
17316  PT_ERRORc (parser, o1, er_msg ());
17317  return 0;
17318  }
17319  else
17320  {
17321  return 1;
17322  }
17323  }
17324  else
17325  {
17326  error = db_string_substring (pt_misc_to_qp_misc_operand (qualifier), arg1, arg2, arg3, result);
17327  if (error < 0)
17328  {
17329  PT_ERRORc (parser, o1, er_msg ());
17330  return 0;
17331  }
17332  else
17333  {
17334  return 1;
17335  }
17336  }
17337 
17338  case PT_OCTET_LENGTH:
17339  if (o1->type_enum == PT_TYPE_NA || o1->type_enum == PT_TYPE_NULL)
17340  {
17341  db_make_null (result);
17342  return 1;
17343  }
17344 
17345  if (!PT_IS_STRING_TYPE (o1->type_enum))
17346  {
17347  return 0;
17348  }
17349 
17350  db_make_int (result, db_get_string_size (arg1));
17351  return 1;
17352 
17353  case PT_BIT_LENGTH:
17354  if (o1->type_enum == PT_TYPE_NA || o1->type_enum == PT_TYPE_NULL)
17355  {
17356  db_make_null (result);
17357  return 1;
17358  }
17359 
17360  if (!PT_IS_STRING_TYPE (o1->type_enum))
17361  {
17362  return 0;
17363  }
17364 
17366  {
17367  db_make_int (result, 8 * db_get_string_size (arg1));
17368  }
17369  else
17370  {
17371  int len = 0;
17372 
17373  /* must be a bit gadget */
17374  (void) db_get_bit (arg1, &len);
17375  db_make_int (result, len);
17376  }
17377  return 1;
17378 
17379  case PT_CHAR_LENGTH:
17380  if (o1->type_enum == PT_TYPE_NA || o1->type_enum == PT_TYPE_NULL)
17381  {
17382  db_make_null (result);
17383  return 1;
17384  }
17385  else if (!PT_IS_CHAR_STRING_TYPE (o1->type_enum))
17386  {
17387  return 0;
17388  }
17389  db_make_int (result, db_get_string_length (arg1));
17390  return 1;
17391 
17392  case PT_LOWER:
17393  error = db_string_lower (arg1, result);
17394  if (error < 0)
17395  {
17396  PT_ERRORc (parser, o1, er_msg ());
17397  return 0;
17398  }
17399  else
17400  {
17401  return 1;
17402  }
17403 
17404  case PT_UPPER:
17405  error = db_string_upper (arg1, result);
17406  if (error < 0)
17407  {
17408  PT_ERRORc (parser, o1, er_msg ());
17409  return 0;
17410  }
17411  else
17412  {
17413  return 1;
17414  }
17415 
17416  case PT_HEX:
17417  error = db_hex (arg1, result);
17418  if (error < 0)
17419  {
17420  PT_ERRORc (parser, o1, er_msg ());
17421  return 0;
17422  }
17423  else
17424  {
17425  return 1;
17426  }
17427 
17428  case PT_ASCII:
17429  error = db_ascii (arg1, result);
17430  if (error < 0)
17431  {
17432  PT_ERRORc (parser, o1, er_msg ());
17433  return 0;
17434  }
17435  else
17436  {
17437  return 1;
17438  }
17439 
17440  case PT_CONV:
17441  error = db_conv (arg1, arg2, arg3, result);
17442  if (error < 0)
17443  {
17444  PT_ERRORc (parser, o1, er_msg ());
17445  return 0;
17446  }
17447  else
17448  {
17449  return 1;
17450  }
17451 
17452  case PT_BIN:
17453  error = db_bigint_to_binary_string (arg1, result);
17454  if (error != NO_ERROR)
17455  {
17456  PT_ERRORc (parser, o1, er_msg ());
17457  return 0;
17458  }
17459  else
17460  {
17461  return 1;
17462  }
17463 
17464  case PT_TRIM:
17465  error = db_string_trim (pt_misc_to_qp_misc_operand (qualifier), arg2, arg1, result);
17466  if (error < 0)
17467  {
17468  PT_ERRORc (parser, o1, er_msg ());
17469  return 0;
17470  }
17471  else
17472  {
17473  return 1;
17474  }
17475 
17476  case PT_LIKE_LOWER_BOUND:
17477  case PT_LIKE_UPPER_BOUND:
17478  error = db_like_bound (arg1, arg2, result, (op == PT_LIKE_LOWER_BOUND));
17479  if (error < 0)
17480  {
17481  PT_ERRORc (parser, o1, er_msg ());
17482  return 0;
17483  }
17484  else
17485  {
17486  return 1;
17487  }
17488 
17489  case PT_LTRIM:
17490  error = db_string_trim (LEADING, arg2, arg1, result);
17491  if (error < 0)
17492  {
17493  PT_ERRORc (parser, o1, er_msg ());
17494  return 0;
17495  }
17496  else
17497  {
17498  return 1;
17499  }
17500 
17501  case PT_RTRIM:
17502  error = db_string_trim (TRAILING, arg2, arg1, result);
17503  if (error < 0)
17504  {
17505  PT_ERRORc (parser, o1, er_msg ());
17506  return 0;
17507  }
17508  else
17509  {
17510  return 1;
17511  }
17512 
17513  case PT_FROM_UNIXTIME:
17514  error = db_from_unixtime (arg1, arg2, arg3, result, domain);
17515  if (error < 0)
17516  {
17517  PT_ERRORc (parser, o1, er_msg ());
17518  return 0;
17519  }
17520  else
17521  {
17522  return 1;
17523  }
17524 
17525  case PT_SUBSTRING_INDEX:
17526  error = db_string_substring_index (arg1, arg2, arg3, result);
17527  if (error < 0)
17528  {
17529  PT_ERRORc (parser, o1, er_msg ());
17530  return 0;
17531  }
17532  break;
17533 
17534  case PT_MD5:
17535  error = db_string_md5 (arg1, result);
17536  if (error < 0)
17537  {
17538  PT_ERRORc (parser, o1, er_msg ());
17539  return 0;
17540  }
17541  else
17542  {
17543  return 1;
17544  }
17545 
17546  case PT_SHA_ONE:
17547  error = db_string_sha_one (arg1, result);
17548  if (error < 0)
17549  {
17550  PT_ERRORc (parser, o1, er_msg ());
17551  return 0;
17552  }
17553  else
17554  {
17555  return 1;
17556  }
17557 
17558  case PT_AES_ENCRYPT:
17559  error = db_string_aes_encrypt (arg1, arg2, result);
17560  if (error < 0)
17561  {
17562  PT_ERRORc (parser, o1, er_msg ());
17563  return 0;
17564  }
17565  else
17566  {
17567  return 1;
17568  }
17569 
17570  case PT_AES_DECRYPT:
17571  error = db_string_aes_decrypt (arg1, arg2, result);
17572  if (error < 0)
17573  {
17574  PT_ERRORc (parser, o1, er_msg ());
17575  return 0;
17576  }
17577  else
17578  {
17579  return 1;
17580  }
17581 
17582  case PT_SHA_TWO:
17583  error = db_string_sha_two (arg1, arg2, result);
17584  if (error < 0)
17585  {
17586  PT_ERRORc (parser, o1, er_msg ());
17587  return 0;
17588  }
17589  else
17590  {
17591  return 1;
17592  }
17593 
17594  case PT_TO_BASE64:
17595  error = db_string_to_base64 (arg1, result);
17596  if (error < 0)
17597  {
17598  PT_ERRORc (parser, o1, er_msg ());
17599  return 0;
17600  }
17601  else
17602  {
17603  return 1;
17604  }
17605 
17606  case PT_FROM_BASE64:
17607  error = db_string_from_base64 (arg1, result);
17608  if (error < 0)
17609  {
17610  PT_ERRORc (parser, o1, er_msg ());
17611  return 0;
17612  }
17613  else
17614  {
17615  return 1;
17616  }
17617 
17618  case PT_LPAD:
17619  error = db_string_pad (LEADING, arg1, arg2, arg3, result);
17620  if (error < 0)
17621  {
17622  PT_ERRORc (parser, o1, er_msg ());
17623  return 0;
17624  }
17625  else
17626  {
17627  return 1;
17628  }
17629 
17630  case PT_RPAD:
17631  error = db_string_pad (TRAILING, arg1, arg2, arg3, result);
17632  if (error < 0)
17633  {
17634  PT_ERRORc (parser, o1, er_msg ());
17635  return 0;
17636  }
17637  else
17638  {
17639  return 1;
17640  }
17641 
17642  case PT_REPLACE:
17643  error = db_string_replace (arg1, arg2, arg3, result);
17644  if (error < 0)
17645  {
17646  PT_ERRORc (parser, o1, er_msg ());
17647  return 0;
17648  }
17649  else
17650  {
17651  return 1;
17652  }
17653 
17654  case PT_TRANSLATE:
17655  error = db_string_translate (arg1, arg2, arg3, result);
17656  if (error < 0)
17657  {
17658  PT_ERRORc (parser, o1, er_msg ());
17659  return 0;
17660  }
17661  else
17662  {
17663  return 1;
17664  }
17665 
17666  case PT_ADD_MONTHS:
17667  error = db_add_months (arg1, arg2, result);
17668  if (error < 0)
17669  {
17670  PT_ERRORc (parser, o1, er_msg ());
17671  return 0;
17672  }
17673  else
17674  {
17675  return 1;
17676  }
17677 
17678  case PT_LAST_DAY:
17679  error = db_last_day (arg1, result);
17680  if (error < 0)
17681  {
17682  PT_ERRORc (parser, expr, er_msg ());
17683  return 0;
17684  }
17685  else
17686  {
17687  return 1;
17688  }
17689 
17690  case PT_UNIX_TIMESTAMP:
17691  error = db_unix_timestamp (arg1, result);
17692  if (error < 0)
17693  {
17694  PT_ERRORc (parser, o1, er_msg ());
17695  return 0;
17696  }
17697  else
17698  {
17699  return 1;
17700  }
17701 
17702  case PT_STR_TO_DATE:
17703  error = db_str_to_date (arg1, arg2, arg3, result, NULL);
17704  if (error < 0)
17705  {
17706  PT_ERRORc (parser, o1, er_msg ());
17707  return 0;
17708  }
17709  else
17710  {
17711  return 1;
17712  }
17713 
17714  case PT_TIME_FORMAT:
17715  error = db_time_format (arg1, arg2, arg3, result, domain);
17716  if (error < 0)
17717  {
17718  PT_ERRORc (parser, o1, er_msg ());
17719  return 0;
17720  }
17721  else
17722  {
17723  return 1;
17724  }
17725 
17726  case PT_TIMESTAMP:
17727  if (typ1 == DB_TYPE_NULL)
17728  {
17729  db_make_null (result);
17730  return 1;
17731  }
17732 
17733  error = db_timestamp (arg1, arg2, result);
17734  if (error < 0)
17735  {
17736  PT_ERRORc (parser, o1, er_msg ());
17737  return 0;
17738  }
17739  else
17740  {
17741  return 1;
17742  }
17743 
17744  case PT_YEARF:
17745  case PT_MONTHF:
17746  case PT_DAYF:
17747  error = db_get_date_item (arg1, op, result);
17748  if (error < 0)
17749  {
17750  PT_ERRORc (parser, o1, er_msg ());
17751  return 0;
17752  }
17753  else
17754  {
17755  return 1;
17756  }
17757 
17758  case PT_DAYOFMONTH:
17759  /* day of month is handled like PT_DAYF */
17760  error = db_get_date_item (arg1, PT_DAYF, result);
17761  if (error < 0)
17762  {
17763  PT_ERRORc (parser, o1, er_msg ());
17764  return 0;
17765  }
17766  else
17767  {
17768  return 1;
17769  }
17770 
17771  case PT_HOURF:
17772  case PT_MINUTEF:
17773  case PT_SECONDF:
17774  error = db_get_time_item (arg1, op, result);
17775  if (error < 0)
17776  {
17777  PT_ERRORc (parser, o1, er_msg ());
17778  return 0;
17779  }
17780  else
17781  {
17782  return 1;
17783  }
17784 
17785  case PT_QUARTERF:
17786  {
17787  error = db_get_date_quarter (arg1, result);
17788  if (error < 0)
17789  {
17790  PT_ERRORc (parser, o1, er_msg ());
17791  return 0;
17792  }
17793  else
17794  {
17795  return 1;
17796  }
17797  }
17798 
17799  case PT_WEEKDAY:
17800  case PT_DAYOFWEEK:
17801  {
17802  error = db_get_date_weekday (arg1, op, result);
17803  if (error < 0)
17804  {
17805  PT_ERRORc (parser, o1, er_msg ());
17806  return 0;
17807  }
17808  else
17809  {
17810  return 1;
17811  }
17812  }
17813 
17814  case PT_DAYOFYEAR:
17815  {
17816  error = db_get_date_dayofyear (arg1, result);
17817  if (error < 0)
17818  {
17819  PT_ERRORc (parser, o1, er_msg ());
17820  return 0;
17821  }
17822  else
17823  {
17824  return 1;
17825  }
17826  }
17827 
17828  case PT_TODAYS:
17829  {
17830  error = db_get_date_totaldays (arg1, result);
17831  if (error < 0)
17832  {
17833  PT_ERRORc (parser, o1, er_msg ());
17834  return 0;
17835  }
17836  else
17837  {
17838  return 1;
17839  }
17840  }
17841 
17842  case PT_FROMDAYS:
17843  {
17844  error = db_get_date_from_days (arg1, result);
17845  if (error < 0)
17846  {
17847  PT_ERRORc (parser, o1, er_msg ());
17848  return 0;
17849  }
17850  else
17851  {
17852  return 1;
17853  }
17854  }
17855 
17856  case PT_TIMETOSEC:
17857  {
17858  error = db_convert_time_to_sec (arg1, result);
17859  if (error < 0)
17860  {
17861  PT_ERRORc (parser, o1, er_msg ());
17862  return 0;
17863  }
17864  else
17865  {
17866  return 1;
17867  }
17868  }
17869 
17870  case PT_SECTOTIME:
17871  {
17872  error = db_convert_sec_to_time (arg1, result);
17873  if (error < 0)
17874  {
17875  PT_ERRORc (parser, o1, er_msg ());
17876  return 0;
17877  }
17878  else
17879  {
17880  return 1;
17881  }
17882  }
17883 
17884  case PT_MAKEDATE:
17885  {
17886  error = db_add_days_to_year (arg1, arg2, result);
17887  if (error < 0)
17888  {
17889  PT_ERRORc (parser, o1, er_msg ());
17890  return 0;
17891  }
17892  else
17893  {
17894  return 1;
17895  }
17896  }
17897 
17898  case PT_MAKETIME:
17899  {
17900  error = db_convert_to_time (arg1, arg2, arg3, result);
17901  if (error < 0)
17902  {
17903  PT_ERRORc (parser, o1, er_msg ());
17904  return 0;
17905  }
17906  else
17907  {
17908  return 1;
17909  }
17910  }
17911 
17912  case PT_ADDTIME:
17913  {
17914  error = db_add_time (arg1, arg2, result, NULL);
17915  if (error < 0)
17916  {
17917  PT_ERRORc (parser, o1, er_msg ());
17918  return 0;
17919  }
17920  else
17921  {
17922  return 1;
17923  }
17924  }
17925 
17926  case PT_WEEKF:
17927  {
17928  error = db_get_date_week (arg1, arg2, result);
17929  if (error < 0)
17930  {
17931  PT_ERRORc (parser, o1, er_msg ());
17932  return 0;
17933  }
17934  else
17935  {
17936  return 1;
17937  }
17938  }
17939 
17940  case PT_SCHEMA:
17941  case PT_DATABASE:
17942  db_make_null (result);
17943  error = db_make_string (&tmp_val, db_get_database_name ());
17944  if (error < 0)
17945  {
17946  PT_ERRORc (parser, o1, er_msg ());
17947  return 0;
17948  }
17949 
17950  error = db_value_clone (&tmp_val, result);
17951  if (error < 0)
17952  {
17953  PT_ERRORc (parser, o1, er_msg ());
17954  return 0;
17955  }
17956  else
17957  {
17958  return 1;
17959  }
17960 
17961  case PT_VERSION:
17962  {
17963  db_make_null (result);
17964  error = db_make_string (&tmp_val, db_get_database_version ());
17965  if (error < 0)
17966  {
17967  PT_ERRORc (parser, o1, er_msg ());
17968  return 0;
17969  }
17970 
17971  error = db_value_clone (&tmp_val, result);
17972  if (error < 0)
17973  {
17974  PT_ERRORc (parser, o1, er_msg ());
17975  return 0;
17976  }
17977  else
17978  {
17979  return 1;
17980  }
17981  }
17982 
17983  case PT_MONTHS_BETWEEN:
17984  error = db_months_between (arg1, arg2, result);
17985  if (error < 0)
17986  {
17987  PT_ERRORc (parser, o1, er_msg ());
17988  return 0;
17989  }
17990  else
17991  {
17992  return 1;
17993  }
17994 
17995  case PT_FORMAT:
17996  error = db_format (arg1, arg2, arg3, result, NULL);
17997  if (error < 0)
17998  {
17999  PT_ERRORc (parser, o1, er_msg ());
18000  return 0;
18001  }
18002  else
18003  {
18004  return 1;
18005  }
18006 
18007  case PT_DATE_FORMAT:
18008  error = db_date_format (arg1, arg2, arg3, result, domain);
18009  if (error < 0)
18010  {
18011  PT_ERRORc (parser, o1, er_msg ());
18012  return 0;
18013  }
18014  else
18015  {
18016  return 1;
18017  }
18018 
18019  case PT_ADDDATE:
18020  error = db_date_add_interval_days (result, arg1, arg2);
18021  if (error < 0)
18022  {
18023  PT_ERRORc (parser, o1, er_msg ());
18024  return 0;
18025  }
18026  else
18027  {
18028  return 1;
18029  }
18030 
18031  case PT_DATEDIFF:
18032  error = db_date_diff (arg1, arg2, result);
18033  if (error < 0)
18034  {
18035  PT_ERRORc (parser, o1, er_msg ());
18036  return 0;
18037  }
18038  else
18039  {
18040  return 1;
18041  }
18042  break;
18043 
18044  case PT_TIMEDIFF:
18045  error = db_time_diff (arg1, arg2, result);
18046  if (error < 0)
18047  {
18048  PT_ERRORc (parser, expr, er_msg ());
18049  return 0;
18050  }
18051  else
18052  {
18053  return 1;
18054  }
18055  break;
18056 
18057  case PT_SUBDATE:
18058  error = db_date_sub_interval_days (result, arg1, arg2);
18059  if (error < 0)
18060  {
18061  PT_ERRORc (parser, o1, er_msg ());
18062  return 0;
18063  }
18064  else
18065  {
18066  return 1;
18067  }
18068 
18069  case PT_DATE_ADD:
18070  error = db_date_add_interval_expr (result, arg1, arg2, o3->info.expr.qualifier);
18071  if (error < 0)
18072  {
18073  PT_ERRORc (parser, o1, er_msg ());
18074  return 0;
18075  }
18076  else
18077  {
18078  return 1;
18079  }
18080 
18081  case PT_DATE_SUB:
18082  error = db_date_sub_interval_expr (result, arg1, arg2, o3->info.expr.qualifier);
18083  if (error < 0)
18084  {
18085  PT_ERRORc (parser, o1, er_msg ());
18086  return 0;
18087  }
18088  else
18089  {
18090  return 1;
18091  }
18092 
18093  case PT_SYS_DATE:
18094  {
18095  DB_DATETIME *tmp_datetime;
18096 
18098 
18099  tmp_datetime = db_get_datetime (&parser->sys_datetime);
18100 
18101  db_value_put_encoded_date (result, &tmp_datetime->date);
18102 
18103  return 1;
18104  }
18105 
18106  case PT_UTC_DATE:
18107  {
18108  DB_DATE date;
18109  DB_TIMESTAMP *timestamp;
18110  int year, month, day, hour, minute, second;
18111 
18112  timestamp = db_get_timestamp (&parser->sys_epochtime);
18113  tz_timestamp_decode_no_leap_sec (*timestamp, &year, &month, &day, &hour, &minute, &second);
18114  date = julian_encode (month + 1, day, year);
18115  db_value_put_encoded_date (result, &date);
18116  return 1;
18117  }
18118 
18119  case PT_CURRENT_DATE:
18120  {
18121  TZ_REGION system_tz_region, session_tz_region;
18122  DB_DATETIME *dest_dt, *tmp_datetime;
18123  int err_status = 0;
18124 
18126  tz_get_system_tz_region (&system_tz_region);
18127  tz_get_session_tz_region (&session_tz_region);
18128  tmp_datetime = db_get_datetime (&parser->sys_datetime);
18129  dest_dt = db_get_datetime (&parser->sys_datetime);
18130  err_status =
18131  tz_conv_tz_datetime_w_region (tmp_datetime, &system_tz_region, &session_tz_region, dest_dt, NULL, NULL);
18132  if (err_status != NO_ERROR)
18133  {
18134  return err_status;
18135  }
18136  db_value_put_encoded_date (result, &dest_dt->date);
18137 
18138  return 1;
18139  }
18140 
18141  case PT_UTC_TIME:
18142  {
18143  DB_TIME db_time;
18144  DB_TIMESTAMP *tmp_datetime;
18145 
18146  tmp_datetime = db_get_timestamp (&parser->sys_epochtime);
18147  db_time = (DB_TIME) (*tmp_datetime % SECONDS_OF_ONE_DAY);
18148  db_value_put_encoded_time (result, &db_time);
18149  return 1;
18150  }
18151 
18152  case PT_SYS_TIME:
18153  {
18154  DB_DATETIME *tmp_datetime;
18155  DB_TIME tmp_time;
18156 
18158 
18159  tmp_datetime = db_get_datetime (&parser->sys_datetime);
18160  tmp_time = tmp_datetime->time / 1000;
18161 
18162  db_value_put_encoded_time (result, &tmp_time);
18163 
18164  return 1;
18165  }
18166 
18167  case PT_CURRENT_TIME:
18168  {
18169  DB_DATETIME *tmp_datetime;
18170  DB_TIME cur_time, tmp_time;
18171  const char *t_source, *t_dest;
18172  int err_status = 0, len_source, len_dest;
18173 
18175  t_source = tz_get_system_timezone ();
18176  t_dest = tz_get_session_local_timezone ();
18177  len_source = (int) strlen (t_source);
18178  len_dest = (int) strlen (t_dest);
18179  tmp_datetime = db_get_datetime (&parser->sys_datetime);
18180  tmp_time = tmp_datetime->time / 1000;
18181 
18182  err_status = tz_conv_tz_time_w_zone_name (&tmp_time, t_source, len_source, t_dest, len_dest, &cur_time);
18183  if (err_status != NO_ERROR)
18184  {
18185  return err_status;
18186  }
18187  db_value_put_encoded_time (result, &cur_time);
18188 
18189  return 1;
18190  }
18191 
18192  case PT_SYS_TIMESTAMP:
18193  {
18194  DB_DATETIME *tmp_datetime;
18195  DB_DATE tmp_date = 0;
18196  DB_TIME tmp_time = 0;
18197  DB_TIMESTAMP tmp_timestamp;
18198 
18200 
18201  tmp_datetime = db_get_datetime (&parser->sys_datetime);
18202  tmp_date = tmp_datetime->date;
18203  tmp_time = tmp_datetime->time / 1000;
18204 
18205  (void) db_timestamp_encode_ses (&tmp_date, &tmp_time, &tmp_timestamp, NULL);
18206  db_make_timestamp (result, tmp_timestamp);
18207 
18208  return 1;
18209  }
18210 
18211  case PT_CURRENT_TIMESTAMP:
18212  {
18213  DB_DATETIME *tmp_datetime;
18214  DB_DATE tmp_date = 0;
18215  DB_TIME tmp_time = 0;
18216  DB_TIMESTAMP tmp_timestamp;
18217 
18219 
18220  tmp_datetime = db_get_datetime (&parser->sys_datetime);
18221  tmp_date = tmp_datetime->date;
18222  tmp_time = tmp_datetime->time / 1000;
18223 
18224  (void) db_timestamp_encode_sys (&tmp_date, &tmp_time, &tmp_timestamp, NULL);
18225  db_make_timestamp (result, tmp_timestamp);
18226 
18227  return 1;
18228  }
18229 
18230  case PT_SYS_DATETIME:
18231  {
18232  DB_DATETIME *tmp_datetime;
18233 
18235  tmp_datetime = db_get_datetime (&parser->sys_datetime);
18236 
18237  db_make_datetime (result, tmp_datetime);
18238 
18239  return 1;
18240  }
18241 
18242  case PT_CURRENT_DATETIME:
18243  {
18244  TZ_REGION system_tz_region, session_tz_region;
18245  DB_DATETIME *tmp_datetime, dest_dt;
18246  int err_status;
18247 
18249  tmp_datetime = db_get_datetime (&parser->sys_datetime);
18250  tz_get_system_tz_region (&system_tz_region);
18251  tz_get_session_tz_region (&session_tz_region);
18252  err_status =
18253  tz_conv_tz_datetime_w_region (tmp_datetime, &system_tz_region, &session_tz_region, &dest_dt, NULL, NULL);
18254  if (err_status != NO_ERROR)
18255  {
18256  return err_status;
18257  }
18258 
18259  db_make_datetime (result, &dest_dt);
18260 
18261  return 1;
18262  }
18263 
18264  case PT_CURRENT_USER:
18265  {
18266  const char *username = au_user_name ();
18267 
18268  error = db_make_string_by_const_str (result, username);
18269  db_string_free ((char *) username);
18270  if (error < 0)
18271  {
18272  PT_ERRORc (parser, o1, er_msg ());
18273  return 0;
18274  }
18275  else
18276  {
18277  return 1;
18278  }
18279  }
18280 
18281  case PT_USER:
18282  {
18283  char *user = NULL;
18284 
18285  user = db_get_user_and_host_name ();
18286  db_make_null (result);
18287 
18288  error = db_make_string (&tmp_val, user);
18289  if (error < 0)
18290  {
18291  PT_ERRORc (parser, o1, er_msg ());
18292  db_private_free (NULL, user);
18293  return 0;
18294  }
18295  tmp_val.need_clear = true;
18296 
18297  error = pr_clone_value (&tmp_val, result);
18298  if (error < 0)
18299  {
18300  PT_ERRORc (parser, o1, er_msg ());
18301  return 0;
18302  }
18303  else
18304  {
18305  return 1;
18306  }
18307  }
18308 
18309  case PT_ROW_COUNT:
18310  {
18311  int row_count = db_get_row_count_cache ();
18312  if (row_count == DB_ROW_COUNT_NOT_SET)
18313  {
18314  /* We do not have a cached value of row count. In this case we read it from the server and update the
18315  * cached value */
18316  db_get_row_count (&row_count);
18317  db_update_row_count_cache (row_count);
18318  }
18319  db_make_int (result, row_count);
18320  return 1;
18321  }
18322 
18323  case PT_LAST_INSERT_ID:
18325  {
18326  return 0;
18327  }
18328  else
18329  {
18330  db_value_clone (&tmp_val, result);
18331  return 1;
18332  }
18333 
18335  db_value_clone (&parser->local_transaction_id, result);
18336  return 1;
18337 
18338  case PT_TO_CHAR:
18339  error = db_to_char (arg1, arg2, arg3, result, domain);
18340  if (error < 0)
18341  {
18342  PT_ERRORc (parser, o1, er_msg ());
18343  return 0;
18344  }
18345  else
18346  {
18347  return 1;
18348  }
18349 
18350  case PT_BIT_TO_BLOB:
18351  error = db_bit_to_blob (arg1, result);
18352  if (error < 0)
18353  {
18354  PT_ERRORc (parser, o1, er_msg ());
18355  return 0;
18356  }
18357  else
18358  {
18359  return 1;
18360  }
18361 
18362  case PT_CHAR_TO_BLOB:
18363  error = db_char_to_blob (arg1, result);
18364  if (error < 0)
18365  {
18366  PT_ERRORc (parser, o1, er_msg ());
18367  return 0;
18368  }
18369  else
18370  {
18371  return 1;
18372  }
18373 
18374  case PT_BLOB_FROM_FILE:
18375  error = db_blob_from_file (arg1, result);
18376  if (error < 0)
18377  {
18378  PT_ERRORc (parser, o1, er_msg ());
18379  return 0;
18380  }
18381  else
18382  {
18383  return 1;
18384  }
18385 
18386  case PT_BLOB_TO_BIT:
18387  error = db_blob_to_bit (arg1, arg2, result);
18388  if (error < 0)
18389  {
18390  PT_ERRORc (parser, o1, er_msg ());
18391  return 0;
18392  }
18393  else
18394  {
18395  return 1;
18396  }
18397 
18398  case PT_BLOB_LENGTH:
18399  error = db_blob_length (arg1, result);
18400  if (error < 0)
18401  {
18402  PT_ERRORc (parser, o1, er_msg ());
18403  return 0;
18404  }
18405  else
18406  {
18407  return 1;
18408  }
18409 
18410  case PT_CHAR_TO_CLOB:
18411  error = db_char_to_clob (arg1, result);
18412  if (error < 0)
18413  {
18414  PT_ERRORc (parser, o1, er_msg ());
18415  return 0;
18416  }
18417  else
18418  {
18419  return 1;
18420  }
18421 
18422  case PT_CLOB_FROM_FILE:
18423  error = db_clob_from_file (arg1, result);
18424  if (error < 0)
18425  {
18426  PT_ERRORc (parser, o1, er_msg ());
18427  return 0;
18428  }
18429  else
18430  {
18431  return 1;
18432  }
18433 
18434  case PT_CLOB_TO_CHAR:
18435  error = db_clob_to_char (arg1, arg2, result);
18436  if (error < 0)
18437  {
18438  PT_ERRORc (parser, o1, er_msg ());
18439  return 0;
18440  }
18441  else
18442  {
18443  return 1;
18444  }
18445 
18446  case PT_CLOB_LENGTH:
18447  error = db_clob_length (arg1, result);
18448  if (error < 0)
18449  {
18450  PT_ERRORc (parser, o1, er_msg ());
18451  return 0;
18452  }
18453  else
18454  {
18455  return 1;
18456  }
18457 
18458  case PT_TO_DATE:
18459  error = db_to_date (arg1, arg2, arg3, result);
18460  if (error < 0)
18461  {
18462  PT_ERRORc (parser, o1, er_msg ());
18463  return 0;
18464  }
18465  else
18466  {
18467  return 1;
18468  }
18469 
18470  case PT_TO_TIME:
18471  error = db_to_time (arg1, arg2, arg3, DB_TYPE_TIME, result);
18472  if (error < 0)
18473  {
18474  PT_ERRORc (parser, o1, er_msg ());
18475  return 0;
18476  }
18477  else
18478  {
18479  return 1;
18480  }
18481 
18482  case PT_TO_TIMESTAMP:
18483  error = db_to_timestamp (arg1, arg2, arg3, DB_TYPE_TIMESTAMP, result);
18484  if (error < 0)
18485  {
18486  PT_ERRORc (parser, o1, er_msg ());
18487  return 0;
18488  }
18489  else
18490  {
18491  return 1;
18492  }
18493 
18494  case PT_TO_DATETIME:
18495  error = db_to_datetime (arg1, arg2, arg3, DB_TYPE_DATETIME, result);
18496  if (error < 0)
18497  {
18498  PT_ERRORc (parser, o1, er_msg ());
18499  return 0;
18500  }
18501  else
18502  {
18503  return 1;
18504  }
18505 
18506  case PT_TO_NUMBER:
18507  error = db_to_number (arg1, arg2, arg3, result);
18508  if (error < 0)
18509  {
18510  PT_ERRORc (parser, o1, er_msg ());
18511  return 0;
18512  }
18513  else
18514  {
18515  return 1;
18516  }
18517 
18518  case PT_EVALUATE_VARIABLE:
18519  {
18520  int err = 0;
18522  err = db_get_variable (arg1, result);
18523  if (err != NO_ERROR)
18524  {
18525  PT_ERRORc (parser, o1, er_msg ());
18526  return 0;
18527  }
18528  return 1;
18529  }
18530 
18531  case PT_CAST:
18532  if (TP_DOMAIN_TYPE (domain) == DB_TYPE_VARIABLE)
18533  {
18534  return 0;
18535  }
18536  dom_status = tp_value_cast (arg1, result, domain, false);
18537  if (dom_status != DOMAIN_COMPATIBLE)
18538  {
18539  assert (expr->node_type == PT_EXPR);
18541  {
18542  db_make_null (result);
18543  return 1;
18544  }
18545  if (er_errid () != NO_ERROR)
18546  {
18547  PT_ERRORc (parser, o1, er_msg ());
18548  }
18550  pt_short_print (parser, o1), pt_show_type_enum (rTyp));
18551  return 0;
18552  }
18553  else
18554  {
18555  return 1;
18556  }
18557 
18558  case PT_CASE:
18559  case PT_DECODE:
18560  /* If arg3 = NULL, then arg2 = NULL and arg1 != NULL. For this case, we've already finished checking
18561  * case_search_condition. */
18562  if (arg3 && (DB_VALUE_TYPE (arg3) == DB_TYPE_INTEGER && db_get_int (arg3) != 0))
18563  {
18564  if (tp_value_coerce (arg1, result, domain) != DOMAIN_COMPATIBLE)
18565  {
18567  pt_short_print (parser, o1), pt_show_type_enum (rTyp));
18568  return 0;
18569  }
18570  }
18571  else
18572  {
18573  if (tp_value_coerce (arg2, result, domain) != DOMAIN_COMPATIBLE)
18574  {
18576  pt_short_print (parser, o2), pt_show_type_enum (rTyp));
18577  return 0;
18578  }
18579  }
18580  break;
18581 
18582  case PT_NULLIF:
18583  if (tp_value_compare (arg1, arg2, 1, 0) == DB_EQ)
18584  {
18585  db_make_null (result);
18586  }
18587  else
18588  {
18589  pr_clone_value ((DB_VALUE *) arg1, result);
18590  }
18591  return 1;
18592 
18593  case PT_EXTRACT:
18594  if (typ1 == DB_TYPE_NULL)
18595  {
18596  db_make_null (result);
18597  }
18598  else
18599  {
18600  MISC_OPERAND q_qualifier;
18601  TP_DOMAIN *domain_p;
18602 
18603  q_qualifier = pt_misc_to_qp_misc_operand (qualifier);
18605 
18606  if (q_qualifier == (MISC_OPERAND) 0)
18607  {
18608  return 0;
18609  }
18610 
18611  if (db_string_extract_dbval (q_qualifier, arg1, result, domain_p) != NO_ERROR)
18612  {
18613  return 0;
18614  }
18615  } /* else */
18616  break;
18617 
18618  case PT_EQ:
18619  case PT_NE:
18620  case PT_GE:
18621  case PT_GT:
18622  case PT_LT:
18623  case PT_LE:
18624 
18625  case PT_SETEQ:
18626  case PT_SETNEQ:
18627  case PT_SUPERSETEQ:
18628  case PT_SUPERSET:
18629  case PT_SUBSETEQ:
18630  case PT_SUBSET:
18631 
18632  case PT_NULLSAFE_EQ:
18633 
18634  case PT_IS_IN:
18635  case PT_IS_NOT_IN:
18636  case PT_EQ_SOME:
18637  case PT_NE_SOME:
18638  case PT_GT_SOME:
18639  case PT_GE_SOME:
18640  case PT_LT_SOME:
18641  case PT_LE_SOME:
18642  case PT_EQ_ALL:
18643  case PT_NE_ALL:
18644  case PT_GT_ALL:
18645  case PT_GE_ALL:
18646  case PT_LT_ALL:
18647  case PT_LE_ALL:
18648  case PT_LIKE:
18649  case PT_NOT_LIKE:
18650  case PT_RLIKE:
18651  case PT_NOT_RLIKE:
18652  case PT_RLIKE_BINARY:
18653  case PT_NOT_RLIKE_BINARY:
18654  case PT_BETWEEN:
18655  case PT_NOT_BETWEEN:
18656  case PT_RANGE:
18657 
18658  if (op != PT_BETWEEN && op != PT_NOT_BETWEEN && op != PT_RANGE && (op != PT_EQ || qualifier != PT_EQ_TORDER))
18659  {
18660  if ((typ1 == DB_TYPE_NULL || typ2 == DB_TYPE_NULL) && op != PT_NULLSAFE_EQ)
18661  {
18662  db_make_null (result); /* NULL comp_op any = NULL */
18663  break;
18664  }
18665  }
18666 
18667  switch (op)
18668  {
18669  case PT_EQ:
18670  if (qualifier == PT_EQ_TORDER)
18671  {
18672  cmp_result = tp_value_compare (arg1, arg2, 1, 1);
18673  cmp = (cmp_result == DB_UNK) ? -1 : (cmp_result == DB_EQ) ? 1 : 0;
18674  break;
18675  }
18676 
18677  /* fall through */
18678  case PT_SETEQ:
18679  cmp_result = (DB_VALUE_COMPARE_RESULT) db_value_compare (arg1, arg2);
18680  cmp = (cmp_result == DB_UNK) ? -1 : (cmp_result == DB_EQ) ? 1 : 0;
18681  break;
18682 
18683  case PT_NE:
18684  case PT_SETNEQ:
18685  cmp_result = (DB_VALUE_COMPARE_RESULT) db_value_compare (arg1, arg2);
18686  cmp = (cmp_result == DB_UNK) ? -1 : (cmp_result != DB_EQ) ? 1 : 0;
18687  break;
18688 
18689  case PT_NULLSAFE_EQ:
18690  if ((o1 && o1->node_type != PT_VALUE) || (o2 && o2->node_type != PT_VALUE))
18691  {
18692  return 0;
18693  }
18694  if (arg1 == NULL || arg1->domain.general_info.is_null)
18695  {
18696  if (arg2 == NULL || arg2->domain.general_info.is_null)
18697  {
18698  cmp_result = DB_EQ;
18699  }
18700  else
18701  {
18702  cmp_result = DB_NE;
18703  }
18704  }
18705  else
18706  {
18707  if (arg2 == NULL || arg2->domain.general_info.is_null)
18708  {
18709  cmp_result = DB_NE;
18710  }
18711  else
18712  {
18713  cmp_result = (DB_VALUE_COMPARE_RESULT) db_value_compare (arg1, arg2);
18714  }
18715  }
18716  cmp = (cmp_result == DB_EQ) ? 1 : 0;
18717  break;
18718 
18719  case PT_SUPERSETEQ:
18720  cmp_result = (DB_VALUE_COMPARE_RESULT) db_set_compare (arg1, arg2);
18721  cmp = (cmp_result == DB_UNK) ? -1 : (cmp_result == DB_EQ || cmp_result == DB_SUPERSET) ? 1 : 0;
18722  break;
18723 
18724  case PT_SUPERSET:
18725  cmp_result = (DB_VALUE_COMPARE_RESULT) db_set_compare (arg1, arg2);
18726  cmp = (cmp_result == DB_UNK) ? -1 : (cmp_result == DB_SUPERSET) ? 1 : 0;
18727  break;
18728 
18729  case PT_SUBSET:
18730  cmp_result = (DB_VALUE_COMPARE_RESULT) db_set_compare (arg1, arg2);
18731  cmp = (cmp_result == DB_UNK) ? -1 : (cmp_result == DB_SUBSET) ? 1 : 0;
18732  break;
18733 
18734  case PT_SUBSETEQ:
18735  cmp_result = (DB_VALUE_COMPARE_RESULT) db_set_compare (arg1, arg2);
18736  cmp = (cmp_result == DB_UNK) ? -1 : (cmp_result == DB_EQ || cmp_result == DB_SUBSET) ? 1 : 0;
18737  break;
18738 
18739  case PT_GE:
18740  cmp_result = (DB_VALUE_COMPARE_RESULT) db_value_compare (arg1, arg2);
18741  cmp = (cmp_result == DB_UNK) ? -1 : (cmp_result == DB_EQ || cmp_result == DB_GT) ? 1 : 0;
18742  break;
18743 
18744  case PT_GT:
18745  cmp_result = (DB_VALUE_COMPARE_RESULT) db_value_compare (arg1, arg2);
18746  cmp = (cmp_result == DB_UNK) ? -1 : (cmp_result == DB_GT) ? 1 : 0;
18747  break;
18748 
18749  case PT_LE:
18750  cmp_result = (DB_VALUE_COMPARE_RESULT) db_value_compare (arg1, arg2);
18751  cmp = (cmp_result == DB_UNK) ? -1 : (cmp_result == DB_EQ || cmp_result == DB_LT) ? 1 : 0;
18752  break;
18753 
18754  case PT_LT:
18755  cmp_result = (DB_VALUE_COMPARE_RESULT) db_value_compare (arg1, arg2);
18756  cmp = (cmp_result == DB_UNK) ? -1 : (cmp_result == DB_LT) ? 1 : 0;
18757  break;
18758 
18759  case PT_EQ_SOME:
18760  case PT_IS_IN:
18761  cmp = set_issome (arg1, db_get_set (arg2), PT_EQ_SOME, 1);
18762  break;
18763 
18764  case PT_NE_SOME:
18765  case PT_GE_SOME:
18766  case PT_GT_SOME:
18767  case PT_LT_SOME:
18768  case PT_LE_SOME:
18769  cmp = set_issome (arg1, db_get_set (arg2), op, 1);
18770  break;
18771 
18772  case PT_EQ_ALL:
18773  cmp = set_issome (arg1, db_get_set (arg2), PT_NE_SOME, 1);
18774  if (cmp == 1)
18775  cmp = 0;
18776  else if (cmp == 0)
18777  cmp = 1;
18778  break;
18779 
18780  case PT_NE_ALL:
18781  case PT_IS_NOT_IN:
18782  cmp = set_issome (arg1, db_get_set (arg2), PT_EQ_SOME, 1);
18783  if (cmp == 1)
18784  cmp = 0;
18785  else if (cmp == 0)
18786  cmp = 1;
18787  break;
18788 
18789  case PT_GE_ALL:
18790  cmp = set_issome (arg1, db_get_set (arg2), PT_LT_SOME, 1);
18791  if (cmp == 1)
18792  cmp = 0;
18793  else if (cmp == 0)
18794  cmp = 1;
18795  break;
18796 
18797  case PT_GT_ALL:
18798  cmp = set_issome (arg1, db_get_set (arg2), PT_LE_SOME, 1);
18799  if (cmp == 1)
18800  cmp = 0;
18801  else if (cmp == 0)
18802  cmp = 1;
18803  break;
18804 
18805  case PT_LT_ALL:
18806  cmp = set_issome (arg1, db_get_set (arg2), PT_GE_SOME, 1);
18807  if (cmp == 1)
18808  cmp = 0;
18809  else if (cmp == 0)
18810  cmp = 1;
18811  break;
18812 
18813  case PT_LE_ALL:
18814  cmp = set_issome (arg1, db_get_set (arg2), PT_GT_SOME, 1);
18815  if (cmp == 1)
18816  cmp = 0;
18817  else if (cmp == 0)
18818  cmp = 1;
18819  break;
18820 
18821  case PT_LIKE:
18822  case PT_NOT_LIKE:
18823  {
18824  DB_VALUE *esc_char = arg3;
18825  DB_VALUE slash_char;
18826  char const *slash_str = "\\";
18827 
18828  if (prm_get_bool_value (PRM_ID_NO_BACKSLASH_ESCAPES) == false && DB_IS_NULL (esc_char))
18829  {
18830  INTL_CODESET arg1_cs = DB_IS_NULL (arg1) ? LANG_SYS_CODESET : db_get_string_codeset (arg1);
18831  int arg1_coll = DB_IS_NULL (arg1) ? LANG_SYS_COLLATION : db_get_string_collation (arg1);
18832  /* when compat_mode=mysql, the slash '\\' is an escape character for LIKE pattern, unless user
18833  * explicitly specifies otherwise. */
18834  esc_char = &slash_char;
18835  if (arg1->domain.general_info.type == DB_TYPE_NCHAR
18837  {
18838  db_make_nchar (esc_char, 1, (const DB_C_NCHAR) slash_str, 1, arg1_cs, arg1_coll);
18839  }
18840  else
18841  {
18842  db_make_char (esc_char, 1, (const DB_C_CHAR) slash_str, 1, arg1_cs, arg1_coll);
18843  }
18844 
18845  esc_char->need_clear = false;
18846  }
18847 
18848  if (db_string_like (arg1, arg2, esc_char, &cmp))
18849  {
18850  /* db_string_like() also checks argument types */
18851  return 0;
18852  }
18853  cmp = ((op == PT_LIKE && cmp == V_TRUE) || (op == PT_NOT_LIKE && cmp == V_FALSE)) ? 1 : 0;
18854  }
18855  break;
18856 
18857  case PT_RLIKE:
18858  case PT_NOT_RLIKE:
18859  case PT_RLIKE_BINARY:
18860  case PT_NOT_RLIKE_BINARY:
18861  {
18862  int err = db_string_rlike (arg1, arg2, arg3, NULL, NULL, &cmp);
18863 
18864  switch (err)
18865  {
18866  case NO_ERROR:
18867  break;
18868  case ER_REGEX_COMPILE_ERROR: /* fall through */
18869  case ER_REGEX_EXEC_ERROR:
18870  PT_ERRORc (parser, o1, er_msg ());
18871  default:
18872  return 0;
18873  }
18874 
18875  /* negate result if using NOT syntax of operator */
18876  if (op == PT_NOT_RLIKE || op == PT_NOT_RLIKE_BINARY)
18877  {
18878  switch (cmp)
18879  {
18880  case V_TRUE:
18881  cmp = V_FALSE;
18882  break;
18883 
18884  case V_FALSE:
18885  cmp = V_TRUE;
18886  break;
18887 
18888  default:
18889  break;
18890  }
18891  }
18892  }
18893  break;
18894 
18895  case PT_BETWEEN:
18896  /* special handling for PT_BETWEEN and PT_NOT_BETWEEN */
18897  cmp_result = (DB_VALUE_COMPARE_RESULT) db_value_compare (arg2, arg1);
18898  cmp_result2 = (DB_VALUE_COMPARE_RESULT) db_value_compare (arg1, arg3);
18899  if (((cmp_result == DB_UNK) && (cmp_result2 == DB_UNK))
18900  || ((cmp_result == DB_UNK) && ((cmp_result2 == DB_LT) || (cmp_result2 == DB_EQ)))
18901  || ((cmp_result2 == DB_UNK) && ((cmp_result == DB_LT) || (cmp_result == DB_EQ))))
18902  {
18903  cmp = -1;
18904  }
18905  else if (((cmp_result != DB_UNK) && (!((cmp_result == DB_LT) || (cmp_result == DB_EQ))))
18906  || ((cmp_result2 != DB_UNK) && (!((cmp_result2 == DB_LT) || (cmp_result2 == DB_EQ)))))
18907  {
18908  cmp = 0;
18909  }
18910  else
18911  {
18912  cmp = 1;
18913  }
18914  break;
18915 
18916  case PT_NOT_BETWEEN:
18917  /* special handling for PT_BETWEEN and PT_NOT_BETWEEN */
18918  cmp_result = (DB_VALUE_COMPARE_RESULT) db_value_compare (arg2, arg1);
18919  cmp_result2 = (DB_VALUE_COMPARE_RESULT) db_value_compare (arg1, arg3);
18920  if (((cmp_result == DB_UNK) && (cmp_result2 == DB_UNK))
18921  || ((cmp_result == DB_UNK) && ((cmp_result2 == DB_LT) || (cmp_result2 == DB_EQ)))
18922  || ((cmp_result2 == DB_UNK) && ((cmp_result == DB_LT) || (cmp_result == DB_EQ))))
18923  {
18924  cmp = -1;
18925  }
18926  else if (((cmp_result != DB_UNK) && (!((cmp_result == DB_LT) || (cmp_result == DB_EQ))))
18927  || ((cmp_result2 != DB_UNK) && (!((cmp_result2 == DB_LT) || (cmp_result2 == DB_EQ)))))
18928  {
18929  cmp = 1;
18930  }
18931  else
18932  {
18933  cmp = 0;
18934  }
18935  break;
18936 
18937  case PT_RANGE:
18938  break;
18939 
18940  default:
18941  return 0;
18942  }
18943 
18944  if (cmp == 1)
18945  db_make_int (result, 1);
18946  else if (cmp == 0)
18947  db_make_int (result, 0);
18948  else
18949  db_make_null (result);
18950  break;
18951 
18952  case PT_INDEX_CARDINALITY:
18953  /* constant folding for this expression is never performed : is always resolved on server */
18954  return 0;
18955  case PT_LIST_DBS:
18956  case PT_SYS_GUID:
18957  case PT_ASSIGN:
18958  case PT_LIKE_ESCAPE:
18959  case PT_BETWEEN_AND:
18960  case PT_BETWEEN_GE_LE:
18961  case PT_BETWEEN_GE_LT:
18962  case PT_BETWEEN_GT_LE:
18963  case PT_BETWEEN_GT_LT:
18964  case PT_BETWEEN_EQ_NA:
18965  case PT_BETWEEN_INF_LE:
18966  case PT_BETWEEN_INF_LT:
18967  case PT_BETWEEN_GE_INF:
18968  case PT_BETWEEN_GT_INF:
18969  /* these don't need to be handled */
18970  return 0;
18971 
18973  {
18974  TP_DOMAIN *enum_domain = NULL;
18975 
18976  assert (expr->info.expr.arg1 != NULL);
18977  assert (expr->data_type != NULL);
18978 
18979  enum_domain = pt_data_type_to_db_domain (parser, expr->data_type, NULL);
18980  if (enum_domain == NULL)
18981  {
18982  return 0;
18983  }
18984 
18985  enum_domain = tp_domain_cache (enum_domain);
18986 
18987  error = db_value_to_enumeration_value (arg1, result, enum_domain);
18988  if (error < 0)
18989  {
18990  PT_ERRORc (parser, o1, er_msg ());
18991  return 0;
18992  }
18993  else
18994  {
18995  if (db_get_enum_short (result) == DB_ENUM_OVERFLOW_VAL)
18996  {
18997  /* To avoid coercing result to enumeration type later on, we consider that this expression cannot be
18998  * folded */
18999  return 0;
19000  }
19001  else
19002  {
19003  return 1;
19004  }
19005  }
19006  }
19007  break;
19008 
19009  case PT_INET_ATON:
19010  error = db_inet_aton (result, arg1);
19011  if (error != NO_ERROR)
19012  {
19013  PT_ERRORc (parser, o1, er_msg ());
19014  return 0;
19015  }
19016  break;
19017 
19018  case PT_INET_NTOA:
19019  error = db_inet_ntoa (result, arg1);
19020  if (error != NO_ERROR)
19021  {
19022  PT_ERRORc (parser, o1, er_msg ());
19023  return 0;
19024  }
19025  break;
19026 
19027  case PT_COERCIBILITY:
19028  /* this expression should always be folded to constant */
19029  assert (false);
19030  break;
19031 
19032  case PT_CHARSET:
19033  case PT_COLLATION:
19034  error = db_get_cs_coll_info (result, arg1, (op == PT_CHARSET) ? 0 : 1);
19035  if (error != NO_ERROR)
19036  {
19037  PT_ERRORc (parser, o1, er_msg ());
19038  return 0;
19039  }
19040  break;
19041 
19042  case PT_WIDTH_BUCKET:
19043  between_ge_lt = o2->info.expr.arg2;
19044  assert (between_ge_lt != NULL && between_ge_lt->node_type == PT_EXPR
19045  && between_ge_lt->info.expr.op == PT_BETWEEN_GE_LT);
19046 
19047  between_ge_lt_arg1 = between_ge_lt->info.expr.arg1;
19048  assert (between_ge_lt_arg1 != NULL && between_ge_lt_arg1->node_type == PT_VALUE);
19049 
19050  between_ge_lt_arg2 = between_ge_lt->info.expr.arg2;
19051  assert (between_ge_lt_arg2 != NULL && between_ge_lt_arg2->node_type == PT_VALUE);
19052 
19053  width_bucket_arg2 = pt_value_to_db (parser, between_ge_lt_arg1);
19054  if (width_bucket_arg2 == NULL)
19055  {
19056  /* error is set in pt_value_to_db */
19057  return 0;
19058  }
19059  width_bucket_arg3 = pt_value_to_db (parser, between_ge_lt_arg2);
19060  if (width_bucket_arg3 == NULL)
19061  {
19062  return 0;
19063  }
19064 
19065  /* get all the parameters */
19066  error = db_width_bucket (result, arg1, width_bucket_arg2, width_bucket_arg3, arg3);
19067  if (error != NO_ERROR)
19068  {
19069  PT_ERRORc (parser, o1, er_msg ());
19070  return 0;
19071  }
19072  break;
19073 
19074  case PT_INDEX_PREFIX:
19075  error = db_string_index_prefix (arg1, arg2, arg3, result);
19076  if (error < 0)
19077  {
19078  PT_ERRORc (parser, o1, er_msg ());
19079  return 0;
19080  }
19081 
19082  break;
19083 
19084  case PT_SLEEP:
19085  error = db_sleep (result, arg1);
19086  if (error < 0)
19087  {
19088  PT_ERRORc (parser, o1, er_msg ());
19089  return 0;
19090  }
19091 
19092  case PT_NEW_TIME:
19093  {
19094  error = db_new_time (arg1, arg2, arg3, result);
19095  if (error < 0)
19096  {
19097  PT_ERRORc (parser, o1, er_msg ());
19098  return 0;
19099  }
19100  else
19101  {
19102  return 1;
19103  }
19104  }
19105 
19106  case PT_FROM_TZ:
19107  {
19108  error = db_from_tz (arg1, arg2, result);
19109  if (error < 0)
19110  {
19111  PT_ERRORc (parser, o1, er_msg ());
19112  return 0;
19113  }
19114  else
19115  {
19116  return 1;
19117  }
19118  }
19119 
19120  case PT_TZ_OFFSET:
19121  {
19122  DB_VALUE timezone;
19123  int timezone_milis;
19124  DB_DATETIME *tmp_datetime, utc_datetime;
19125 
19126  tmp_datetime = db_get_datetime (&parser->sys_datetime);
19127  db_sys_timezone (&timezone);
19128  timezone_milis = db_get_int (&timezone) * 60000;
19129  db_add_int_to_datetime (tmp_datetime, timezone_milis, &utc_datetime);
19130 
19131  if (DB_IS_NULL (arg1))
19132  {
19133  db_make_null (result);
19134  }
19135  else
19136  {
19137  if (db_tz_offset (arg1, result, &utc_datetime) != NO_ERROR)
19138  {
19139  PT_ERRORc (parser, o1, er_msg ());
19140  return 0;
19141  }
19142  }
19143  }
19144  break;
19145 
19146  case PT_TO_DATETIME_TZ:
19147  error = db_to_datetime (arg1, arg2, arg3, DB_TYPE_DATETIMETZ, result);
19148  if (error < 0)
19149  {
19150  PT_ERRORc (parser, o1, er_msg ());
19151  return 0;
19152  }
19153  else
19154  {
19155  return 1;
19156  }
19157  break;
19158 
19159  case PT_CONV_TZ:
19160  error = db_conv_tz (arg1, result);
19161  if (error < 0)
19162  {
19163  PT_ERRORc (parser, o1, er_msg ());
19164  return 0;
19165  }
19166  else
19167  {
19168  return 1;
19169  }
19170  break;
19171 
19172  case PT_TO_TIMESTAMP_TZ:
19173  error = db_to_timestamp (arg1, arg2, arg3, DB_TYPE_TIMESTAMPTZ, result);
19174  if (error < 0)
19175  {
19176  PT_ERRORc (parser, o1, er_msg ());
19177  return 0;
19178  }
19179  else
19180  {
19181  return 1;
19182  }
19183  break;
19184 
19185  case PT_UTC_TIMESTAMP:
19186  {
19187  DB_TIME time;
19188  DB_DATE date;
19189  DB_TIMESTAMP *tmp_timestamp, timestamp;
19190  int year, month, day, hour, minute, second;
19191 
19192  tmp_timestamp = db_get_timestamp (&parser->sys_epochtime);
19193  tz_timestamp_decode_no_leap_sec (*tmp_timestamp, &year, &month, &day, &hour, &minute, &second);
19194  date = julian_encode (month + 1, day, year);
19195  db_time_encode (&time, hour, minute, second);
19196  db_timestamp_encode_ses (&date, &time, &timestamp, NULL);
19197  db_make_timestamp (result, timestamp);
19198  return 1;
19199  }
19200 
19201  case PT_CRC32:
19202  error = db_crc32_dbval (result, arg1);
19203  if (error < 0)
19204  {
19205  PT_ERRORc (parser, o1, er_msg ());
19206  return 0;
19207  }
19208  else
19209  {
19210  return 1;
19211  }
19212  case PT_SCHEMA_DEF:
19213  {
19214  error = db_get_schema_def_dbval (result, arg1);
19215  if (error < 0)
19216  {
19217  const char *table_name = NULL;
19219  {
19220  table_name = db_get_string (arg1);
19221  assert (table_name != NULL);
19222 
19223  if (error == ER_OBJ_NOT_A_CLASS)
19224  {
19227  return 0;
19228  }
19229  else if (error == ER_AU_SELECT_FAILURE)
19230  {
19232  table_name);
19233  return 0;
19234  }
19235  }
19236 
19237  PT_ERRORc (parser, o1, er_msg ());
19238  return 0;
19239  }
19240  else
19241  {
19242  return 1;
19243  }
19244  }
19245 
19246  default:
19247  break;
19248  }
19249 
19250  return 1;
19251 
19252 overflow:
19254  return 0;
19255 
19256 error_zerodate:
19257  PT_ERRORc (parser, o1, er_msg ());
19258  return 0;
19259 }
19260 
19261 /*
19262  * pt_fold_const_expr () - evaluate constant expression
19263  * return: the evaluated expression, if successful,
19264  * unchanged expr, if not successful.
19265  * parser(in): parser global context info for reentrancy
19266  * expr(in): a parse tree representation of a constant expression
19267  */
19268 
19269 static PT_NODE *
19270 pt_fold_const_expr (PARSER_CONTEXT * parser, PT_NODE * expr, void *arg)
19271 {
19272  PT_TYPE_ENUM type1, type2 = PT_TYPE_NONE, type3, result_type;
19273  PT_NODE *opd1 = NULL, *opd2 = NULL, *opd3 = NULL, *result = NULL;
19274  DB_VALUE dummy, dbval_res, *arg1, *arg2, *arg3;
19275  PT_OP_TYPE op;
19276  PT_NODE *expr_next;
19277  int line, column;
19278  short location;
19279  const char *alias_print;
19280  unsigned is_hidden_column;
19281 
19282  // in case of error this functions return the unmodified expr node
19283  // to avoid a memory leak we need to restore the link of expr->next to expr_next
19284  // this will be done in the goto end label
19285  bool has_error = false;
19286  bool was_error_set = false;
19287 
19288  if (expr == NULL)
19289  {
19290  return expr;
19291  }
19292 
19293  if (expr->node_type != PT_EXPR)
19294  {
19295  return expr;
19296  }
19297 
19298  if (expr->do_not_fold)
19299  {
19300  return expr;
19301  }
19302 
19303  location = expr->info.expr.location;
19304 
19305  db_make_null (&dbval_res);
19306 
19307  line = expr->line_number;
19308  column = expr->column_number;
19309  alias_print = expr->alias_print;
19310  expr_next = expr->next;
19311  expr->next = NULL;
19312  result_type = expr->type_enum;
19313  result = expr;
19314  is_hidden_column = expr->is_hidden_column;
19315 
19316  op = expr->info.expr.op;
19317 
19318  if (op == PT_FUNCTION_HOLDER)
19319  {
19320  assert (expr->info.expr.arg1 != NULL);
19321 
19322  if (expr->info.expr.arg1->node_type == PT_VALUE)
19323  {
19324  /* const folding OK , replace current node with arg1 VALUE */
19325  result = parser_copy_tree (parser, expr->info.expr.arg1);
19326  result->info.value.location = location;
19327  result->is_hidden_column = is_hidden_column;
19328  if (result->info.value.text == NULL)
19329  {
19330  result->info.value.text = pt_append_string (parser, NULL, result->alias_print);
19331  }
19332  parser_free_tree (parser, expr);
19333  }
19334  else if (expr->info.expr.arg1->node_type != PT_FUNCTION)
19335  {
19336  assert (false);
19337  }
19338  goto end;
19339  }
19340  /* special handling for only one range - convert to comp op */
19341  if (op == PT_RANGE)
19342  {
19343  PT_NODE *between_and;
19344  PT_OP_TYPE between_op;
19345 
19346  between_and = expr->info.expr.arg2;
19347  between_op = between_and->info.expr.op;
19348  if (between_and->or_next == NULL)
19349  { /* has only one range */
19350  opd1 = expr->info.expr.arg1;
19351  opd2 = between_and->info.expr.arg1;
19352  opd3 = between_and->info.expr.arg2;
19353  if (opd1 && opd1->node_type == PT_VALUE && opd2 && opd2->node_type == PT_VALUE)
19354  { /* both const */
19355  if (between_op == PT_BETWEEN_EQ_NA || between_op == PT_BETWEEN_GT_INF || between_op == PT_BETWEEN_GE_INF
19356  || between_op == PT_BETWEEN_INF_LT || between_op == PT_BETWEEN_INF_LE)
19357  {
19358  /* convert to comp op */
19359  between_and->info.expr.arg1 = NULL;
19360  parser_free_tree (parser, between_and);
19361  expr->info.expr.arg2 = opd2;
19362 
19363  if (between_op == PT_BETWEEN_EQ_NA)
19364  {
19365  op = expr->info.expr.op = PT_EQ;
19366  }
19367  else if (between_op == PT_BETWEEN_GT_INF)
19368  {
19369  op = expr->info.expr.op = PT_GT;
19370  }
19371  else if (between_op == PT_BETWEEN_GE_INF)
19372  {
19373  op = expr->info.expr.op = PT_GE;
19374  }
19375  else if (between_op == PT_BETWEEN_INF_LT)
19376  {
19377  op = expr->info.expr.op = PT_LT;
19378  }
19379  else
19380  {
19381  op = expr->info.expr.op = PT_LE;
19382  }
19383  }
19384  else if (between_op == PT_BETWEEN_GE_LE)
19385  {
19386  if (opd3 && opd3->node_type == PT_VALUE)
19387  {
19388  /* convert to between op */
19389  between_and->info.expr.op = PT_BETWEEN_AND;
19390  op = expr->info.expr.op = PT_BETWEEN;
19391  }
19392  }
19393  }
19394  }
19395  }
19396  else if (op == PT_NEXT_VALUE || op == PT_CURRENT_VALUE || op == PT_BIT_TO_BLOB || op == PT_CHAR_TO_BLOB
19397  || op == PT_BLOB_TO_BIT || op == PT_BLOB_LENGTH || op == PT_CHAR_TO_CLOB || op == PT_CLOB_TO_CHAR
19398  || op == PT_CLOB_LENGTH || op == PT_EXEC_STATS || op == PT_TRACE_STATS || op == PT_TZ_OFFSET)
19399  {
19400  goto end;
19401  }
19402  else if (op == PT_ROW_COUNT)
19403  {
19404  int row_count = db_get_row_count_cache ();
19405  if (row_count == DB_ROW_COUNT_NOT_SET)
19406  {
19407  /* Read the value from the server and cache it */
19408  db_get_row_count (&row_count);
19409  db_update_row_count_cache (row_count);
19410  }
19411  db_make_int (&dbval_res, row_count);
19412  result = pt_dbval_to_value (parser, &dbval_res);
19413  goto end;
19414  }
19415  else if (op == PT_COERCIBILITY)
19416  {
19417  PT_COLL_INFER coll_infer;
19418 
19419  if (pt_get_collation_info (expr->info.expr.arg1, &coll_infer))
19420  {
19421  if (coll_infer.coerc_level >= PT_COLLATION_L2_COERC && coll_infer.coerc_level <= PT_COLLATION_L2_BIN_COERC
19422  && coll_infer.can_force_cs == true)
19423  {
19424  db_make_int (&dbval_res, -1);
19425  }
19426  else
19427  {
19428  db_make_int (&dbval_res, (int) (coll_infer.coerc_level));
19429  }
19430  }
19431  else
19432  {
19434  {
19435  er_clear ();
19436  db_make_null (&dbval_res);
19437  }
19438  else
19439  {
19441  PT_ERRORc (parser, expr, er_msg ());
19442  goto end;
19443  }
19444  }
19445 
19446  result = pt_dbval_to_value (parser, &dbval_res);
19447  goto end;
19448  }
19449 
19450  opd1 = expr->info.expr.arg1;
19451 
19452  if (opd1 && op == PT_DEFAULTF)
19453  {
19454  PT_NODE *default_value, *default_value_date_type;
19455  bool needs_update_precision = false;
19456 
19458  if (default_value == NULL)
19459  {
19460  has_error = true;
19461  goto end;
19462  }
19463 
19464  default_value_date_type = opd1->info.name.default_value->data_type;
19465  if (opd1->data_type != NULL)
19466  {
19467  switch (opd1->type_enum)
19468  {
19469  case PT_TYPE_CHAR:
19470  case PT_TYPE_VARCHAR:
19471  case PT_TYPE_NCHAR:
19472  case PT_TYPE_VARNCHAR:
19473  case PT_TYPE_BIT:
19474  case PT_TYPE_VARBIT:
19475  case PT_TYPE_NUMERIC:
19476  case PT_TYPE_ENUMERATION:
19477  if (default_value_date_type == NULL
19478  || (default_value_date_type->info.data_type.precision != opd1->data_type->info.data_type.precision))
19479  {
19480  needs_update_precision = true;
19481  }
19482  break;
19483 
19484  default:
19485  break;
19486  }
19487  }
19488 
19489  if ((opd1->info.name.default_value->type_enum == PT_TYPE_NULL)
19490  || (opd1->info.name.default_value->type_enum == opd1->type_enum && needs_update_precision == false))
19491  {
19492  result = default_value;
19493  }
19494  else
19495  {
19496  PT_NODE *dt, *cast_expr;
19497 
19498  /* need to coerce to opd1->type_enum */
19499  cast_expr = parser_new_node (parser, PT_EXPR);
19500  if (cast_expr == NULL)
19501  {
19503  has_error = true;
19504 
19505  goto end;
19506  }
19507 
19508  cast_expr->line_number = opd1->info.name.default_value->line_number;
19509  cast_expr->column_number = opd1->info.name.default_value->column_number;
19510  cast_expr->info.expr.op = PT_CAST;
19511  cast_expr->info.expr.arg1 = default_value;
19512  cast_expr->type_enum = opd1->type_enum;
19513  cast_expr->info.expr.location = is_hidden_column;
19514 
19515  if (opd1->data_type)
19516  {
19517  dt = parser_copy_tree (parser, opd1->data_type);
19518  if (dt == NULL)
19519  {
19521  parser_free_tree (parser, cast_expr);
19522 
19523  has_error = true;
19524  goto end;
19525  }
19526  }
19527  else
19528  {
19530  if (dt == NULL)
19531  {
19533  parser_free_tree (parser, cast_expr);
19534 
19535  has_error = true;
19536  goto end;
19537  }
19538  dt->type_enum = opd1->type_enum;
19539  }
19540 
19541  cast_expr->info.expr.cast_type = dt;
19542  result = cast_expr;
19543  }
19544 
19545  goto end;
19546  }
19547 
19548  if (op == PT_OID_OF_DUPLICATE_KEY)
19549  {
19550  OID null_oid;
19551  PT_NODE *tmp_value = parser_new_node (parser, PT_VALUE);
19552 
19553  if (tmp_value == NULL)
19554  {
19555  has_error = true;
19556  goto end;
19557  }
19558 
19559  /* a NULL OID is returned; the resulting PT_VALUE node will be replaced with a PT_HOST_VAR by the auto
19560  * parameterization step because of the special force_auto_parameterize flag. Also see and pt_dup_key_update_stmt
19561  * () and qo_optimize_queries () */
19562  tmp_value->type_enum = PT_TYPE_OBJECT;
19563  OID_SET_NULL (&null_oid);
19564  db_make_oid (&tmp_value->info.value.db_value, &null_oid);
19565  tmp_value->info.value.db_value_is_initialized = true;
19566  tmp_value->data_type = parser_copy_tree (parser, expr->data_type);
19567  if (tmp_value->data_type == NULL)
19568  {
19569  parser_free_tree (parser, tmp_value);
19570  tmp_value = NULL;
19571  has_error = true;
19572  goto end;
19573  }
19574  result = tmp_value;
19575  goto end;
19576  }
19577 
19578  if (opd1 && opd1->node_type == PT_VALUE)
19579  {
19580  arg1 = pt_value_to_db (parser, opd1);
19581  type1 = opd1->type_enum;
19582  }
19583  else
19584  {
19585  if (op == PT_EQ && expr->info.expr.qualifier == PT_EQ_TORDER)
19586  {
19587  goto end;
19588  }
19589  db_make_null (&dummy);
19590  arg1 = &dummy;
19591  type1 = PT_TYPE_NULL;
19592  }
19593 
19594  /* special handling for PT_BETWEEN and PT_NOT_BETWEEN */
19595  if ((op == PT_BETWEEN || op == PT_NOT_BETWEEN)
19597  {
19598  opd2 = expr->info.expr.arg2->info.expr.arg1;
19599  }
19600  else
19601  {
19602  opd2 = expr->info.expr.arg2;
19603  }
19604 
19605  if (opd2 && opd2->node_type == PT_VALUE)
19606  {
19607  arg2 = pt_value_to_db (parser, opd2);
19608  type2 = opd2->type_enum;
19609  }
19610  else
19611  {
19612  switch (op)
19613  {
19614  case PT_TRIM:
19615  case PT_LTRIM:
19616  case PT_RTRIM:
19617  {
19618  arg2 = NULL;
19619  }
19620  break;
19621 
19622  case PT_FROM_UNIXTIME:
19623  arg2 = NULL;
19624  break;
19625 
19626  case PT_LIKE_LOWER_BOUND:
19627  case PT_LIKE_UPPER_BOUND:
19628  case PT_TIMESTAMP:
19629  /* If an operand2 exists and it's not a value, do not fold. */
19630  if (opd2 != NULL)
19631  {
19632  goto end;
19633  }
19634  arg2 = NULL;
19635  break;
19636 
19637  case PT_INCR:
19638  case PT_DECR:
19639  {
19640  PT_NODE *entity = NULL, *top, *spec;
19641  PT_NODE *dtype;
19642  const char *attr_name;
19643  SEMANTIC_CHK_INFO *sc_info = (SEMANTIC_CHK_INFO *) arg;
19644  int attrid, shared;
19645  DB_DOMAIN *dom;
19646 
19647  /* add an argument, oid of instance to do increment */
19648  if (opd1 != NULL && opd1->node_type == PT_NAME)
19649  {
19650  opd2 = pt_name (parser, "");
19651  if (opd2 == NULL)
19652  {
19653  has_error = true;
19654  goto end;
19655  }
19656 
19657  dtype = parser_new_node (parser, PT_DATA_TYPE);
19658  if (dtype == NULL)
19659  {
19660  has_error = true;
19661  goto end;
19662  }
19663 
19664  if (sc_info && (top = sc_info->top_node) && (top->node_type == PT_SELECT))
19665  {
19666  /* if given top node, find domain class, and check if it is a derived class */
19667  spec = pt_find_entity (parser, top->info.query.q.select.from, opd1->info.name.spec_id);
19668  if (spec)
19669  {
19670  entity = spec->info.spec.entity_name;
19671  }
19672  }
19673  else
19674  {
19675  entity = pt_name (parser, opd1->info.name.resolved);
19676  if (entity == NULL)
19677  {
19678  has_error = true;
19679  goto end;
19680  }
19681  entity->info.name.db_object = db_find_class (entity->info.name.original);
19682  }
19683 
19684  if (entity == NULL || entity->info.name.db_object == NULL)
19685  {
19686  PT_ERRORf (parser, expr, "Attribute of derived class " "is not permitted in %s()",
19687  (op == PT_INCR ? "INCR" : "DECR"));
19688  has_error = true;
19689  was_error_set = true;
19690  goto end;
19691  }
19692  dtype->type_enum = PT_TYPE_OBJECT;
19693  dtype->info.data_type.entity = entity;
19695 
19696  opd2->data_type = dtype;
19697  opd2->type_enum = PT_TYPE_OBJECT;
19698  opd2->info.name.meta_class = PT_OID_ATTR;
19699  opd2->info.name.spec_id = opd1->info.name.spec_id;
19700  opd2->info.name.resolved = pt_append_string (parser, NULL, opd1->info.name.resolved);
19701  if (opd2->info.name.resolved == NULL)
19702  {
19703  has_error = true;
19704  goto end;
19705  }
19706 
19707  attr_name = opd1->info.name.original;
19708  expr->info.expr.arg2 = opd2;
19709  }
19710  else if (opd1 != NULL && opd1->node_type == PT_DOT_)
19711  {
19712  PT_NODE *arg2, *arg1 = opd1->info.dot.arg1;
19713 
19714  opd2 = parser_copy_tree_list (parser, arg1);
19715  if (opd2 == NULL)
19716  {
19717  has_error = true;
19718  goto end;
19719  }
19720 
19721  if (opd2->node_type == PT_DOT_)
19722  {
19723  arg2 = opd2->info.dot.arg2;
19724  entity = arg2->data_type->info.data_type.entity;
19725  }
19726  else
19727  {
19728  entity = opd2->data_type->info.data_type.entity;
19729  }
19730 
19731  attr_name = opd1->info.dot.arg2->info.name.original;
19732  expr->info.expr.arg2 = opd2;
19733  }
19734  else
19735  {
19736  PT_ERRORf (parser, expr, "Invalid argument in %s()", (op == PT_INCR ? "INCR" : "DECR"));
19737 
19738  has_error = true;
19739  was_error_set = true;
19740  goto end;
19741  }
19742 
19743  /* add an argument, id of attribute to do increment */
19744  opd3 = parser_new_node (parser, PT_VALUE);
19745  if (opd3 == NULL)
19746  {
19747  has_error = true;
19748  goto end;
19749  }
19750 
19751  if (sm_att_info (entity->info.name.db_object, attr_name, &attrid, &dom, &shared, 0) < 0)
19752  {
19753  has_error = true;
19754  goto end;
19755  }
19756 
19757  opd3->type_enum = PT_TYPE_INTEGER;
19758  opd3->info.value.data_value.i = attrid;
19759  expr->info.expr.arg3 = opd3;
19760  }
19761 
19762  /* fall through */
19763  default:
19764  db_make_null (&dummy);
19765  arg2 = &dummy;
19766  type2 = PT_TYPE_NULL;
19767  }
19768  }
19769 
19771  && (op == PT_STRCAT || op == PT_PLUS || op == PT_CONCAT || op == PT_CONCAT_WS))
19772  {
19773  TP_DOMAIN *domain;
19774 
19775  /* use the caching variant of this function ! */
19776  domain = pt_xasl_node_to_domain (parser, expr);
19777 
19778  if (domain && QSTR_IS_ANY_CHAR_OR_BIT (TP_DOMAIN_TYPE (domain)))
19779  {
19780  if (opd1 && opd1->node_type == PT_VALUE && type1 == PT_TYPE_NULL && PT_IS_STRING_TYPE (type2))
19781  {
19782  /* fold 'null || char_opnd' expr to 'char_opnd' */
19783  result = parser_copy_tree (parser, opd2);
19784  if (result == NULL)
19785  {
19786  has_error = true;
19787  goto end;
19788  }
19789  }
19790  else if (opd2 && opd2->node_type == PT_VALUE && type2 == PT_TYPE_NULL && PT_IS_STRING_TYPE (type1))
19791  {
19792  /* fold 'char_opnd || null' expr to 'char_opnd' */
19793  result = parser_copy_tree (parser, opd1);
19794  if (result == NULL)
19795  {
19796  has_error = true;
19797  goto end;
19798  }
19799  }
19800 
19801  goto end; /* finish folding */
19802  }
19803  }
19804 
19805  /* special handling for PT_BETWEEN and PT_NOT_BETWEEN */
19806  if ((op == PT_BETWEEN || op == PT_NOT_BETWEEN)
19808  {
19809  opd3 = expr->info.expr.arg2->info.expr.arg2;
19810  }
19811  else
19812  {
19813  opd3 = expr->info.expr.arg3;
19814  }
19815 
19816  if (opd3 && opd3->node_type == PT_VALUE)
19817  {
19818  arg3 = pt_value_to_db (parser, opd3);
19819  type3 = opd3->type_enum;
19820  }
19821  else
19822  {
19823  switch (op)
19824  {
19825  case PT_LPAD:
19826  case PT_RPAD:
19827  case PT_REPLACE:
19828  {
19829  arg3 = NULL;
19830  }
19831  break;
19832  case PT_LIKE:
19833  arg3 = NULL;
19834  type3 = PT_TYPE_NONE;
19835  break;
19836 
19837  default:
19838  db_make_null (&dummy);
19839  arg3 = &dummy;
19840  type3 = PT_TYPE_NULL;
19841  break;
19842  }
19843  }
19844 
19845  /* If the search condition for the CASE op is already determined, optimize the tree and screen out the arguments for
19846  * a possible call of pt_evaluate_db_val_expr. */
19847  if ((op == PT_CASE || op == PT_DECODE) && opd3 && opd3->node_type == PT_VALUE)
19848  {
19849  if (arg3 && (DB_VALUE_TYPE (arg3) == DB_TYPE_INTEGER && db_get_int (arg3)))
19850  {
19851  opd2 = NULL;
19852  }
19853  else
19854  {
19855  opd1 = opd2;
19856  arg1 = arg2;
19857  opd2 = NULL;
19858  }
19859 
19860  if (opd1 && opd1->node_type != PT_VALUE)
19861  {
19862  if (expr->info.expr.arg2 == opd1 && (opd1->info.expr.op == PT_CASE || opd1->info.expr.op == PT_DECODE))
19863  {
19864  opd1->info.expr.continued_case = 0;
19865  }
19866 
19867  if (pt_check_same_datatype (parser, expr, opd1))
19868  {
19869  result = parser_copy_tree_list (parser, opd1);
19870  if (result == NULL)
19871  {
19872  has_error = true;
19873  goto end;
19874  }
19875  }
19876  else
19877  {
19878  PT_NODE *res;
19879 
19880  res = parser_new_node (parser, PT_EXPR);
19881  if (res == NULL)
19882  {
19883  has_error = true;
19884  goto end;
19885  }
19886 
19887  res->line_number = opd1->line_number;
19888  res->column_number = opd1->column_number;
19889  res->info.expr.op = PT_CAST;
19890  res->info.expr.arg1 = parser_copy_tree_list (parser, opd1);
19891  res->type_enum = expr->type_enum;
19892  res->info.expr.location = expr->info.expr.location;
19893  res->is_hidden_column = is_hidden_column;
19895 
19896  if (pt_is_set_type (expr))
19897  {
19898  PT_NODE *sdt;
19899 
19901  if (sdt == NULL)
19902  {
19903  parser_free_tree (parser, res);
19904  has_error = true;
19905  goto end;
19906  }
19908  sdt->type_enum = expr->type_enum;
19910  res->info.expr.cast_type = sdt;
19911  }
19912  else if (PT_IS_PARAMETERIZED_TYPE (expr->type_enum))
19913  {
19916  res->info.expr.cast_type->type_enum = expr->type_enum;
19917  }
19918  else
19919  {
19920  PT_NODE *dt;
19921 
19923  if (dt == NULL)
19924  {
19925  parser_free_tree (parser, res);
19926  has_error = true;
19927  goto end;
19928  }
19929  dt->type_enum = expr->type_enum;
19930  res->info.expr.cast_type = dt;
19931  }
19932 
19933  result = res;
19934  }
19935  }
19936 
19937  opd3 = NULL;
19938  }
19939 
19940  /* If the op is AND or OR and one argument is a true/false/NULL and the other is a logical expression, optimize the
19941  * tree so that one of the arguments replaces the node. */
19942  if (opd1 && opd2
19943  && ((opd1->type_enum == PT_TYPE_LOGICAL || opd1->type_enum == PT_TYPE_NULL || opd1->type_enum == PT_TYPE_MAYBE)
19944  && (opd2->type_enum == PT_TYPE_LOGICAL || opd2->type_enum == PT_TYPE_NULL
19945  || opd2->type_enum == PT_TYPE_MAYBE))
19946  && ((opd1->node_type == PT_VALUE && opd2->node_type != PT_VALUE)
19947  || (opd2->node_type == PT_VALUE && opd1->node_type != PT_VALUE)) && (op == PT_AND || op == PT_OR))
19948  {
19949  PT_NODE *val;
19950  PT_NODE *other;
19951  DB_VALUE *db_value;
19952 
19953  if (opd1->node_type == PT_VALUE)
19954  {
19955  val = opd1;
19956  other = opd2;
19957  }
19958  else
19959  {
19960  val = opd2;
19961  other = opd1;
19962  }
19963 
19964  db_value = pt_value_to_db (parser, val);
19965  if (op == PT_AND)
19966  {
19968  {
19969  result = val;
19970  }
19971  else if (db_value && db_get_int (db_value) == 1)
19972  {
19973  result = other;
19974  }
19975  else
19976  {
19977  result = val;
19978  }
19979  }
19980  else
19981  { /* op == PT_OR */
19983  {
19984  result = other;
19985  }
19986  else if (db_value && db_get_int (db_value) == 1)
19987  {
19988  result = val;
19989  }
19990  else
19991  {
19992  result = other;
19993  }
19994  }
19995 
19996  result = parser_copy_tree_list (parser, result);
19997  if (result == NULL)
19998  {
19999  has_error = true;
20000  goto end;
20001  }
20002  }
20003  else if (opd1
20004  && ((opd1->node_type == PT_VALUE
20006  && (!opd2 || opd2->node_type == PT_VALUE) && (!opd3 || opd3->node_type == PT_VALUE))
20007  || ((opd1->type_enum == PT_TYPE_NA || opd1->type_enum == PT_TYPE_NULL)
20008  && op != PT_CASE && op != PT_TO_CHAR && op != PT_TO_NUMBER && op != PT_TO_DATE && op != PT_TO_TIME
20009  && op != PT_TO_TIMESTAMP && op != PT_TO_DATETIME && op != PT_NULLIF && op != PT_COALESCE
20010  && op != PT_NVL && op != PT_NVL2 && op != PT_DECODE && op != PT_IFNULL && op != PT_TO_DATETIME_TZ
20011  && op != PT_TO_TIMESTAMP_TZ)
20012  || (opd2 && (opd2->type_enum == PT_TYPE_NA || opd2->type_enum == PT_TYPE_NULL)
20013  && op != PT_CASE && op != PT_TO_CHAR && op != PT_TO_NUMBER && op != PT_TO_DATE && op != PT_TO_TIME
20014  && op != PT_TO_TIMESTAMP && op != PT_TO_DATETIME && op != PT_BETWEEN && op != PT_NOT_BETWEEN
20015  && op != PT_SYS_CONNECT_BY_PATH && op != PT_NULLIF && op != PT_COALESCE && op != PT_NVL
20016  && op != PT_NVL2 && op != PT_DECODE && op != PT_IFNULL && op != PT_IF
20017  && (op != PT_RANGE || !opd2->or_next) && op != PT_TO_DATETIME_TZ && op != PT_TO_TIMESTAMP_TZ)
20018  || (opd3 && (opd3->type_enum == PT_TYPE_NA || opd3->type_enum == PT_TYPE_NULL)
20019  && op != PT_BETWEEN && op != PT_NOT_BETWEEN && op != PT_NVL2 && op != PT_IF)
20020  || (opd2 && opd3 && op == PT_IF && (opd2->type_enum == PT_TYPE_NA || opd2->type_enum == PT_TYPE_NULL)
20021  && (opd3->type_enum == PT_TYPE_NA || opd3->type_enum == PT_TYPE_NULL))
20022  /* width_bucket special case */
20023  || (op == PT_WIDTH_BUCKET && pt_is_const_foldable_width_bucket (parser, expr))))
20024  {
20025  PT_MISC_TYPE qualifier = (PT_MISC_TYPE) 0;
20026  TP_DOMAIN *domain;
20027 
20028  if (op == PT_TRIM || op == PT_EXTRACT || op == PT_SUBSTRING || op == PT_EQ)
20029  {
20030  qualifier = expr->info.expr.qualifier;
20031  }
20032 
20033  /* use the caching variant of this function ! */
20034  domain = pt_xasl_node_to_domain (parser, expr);
20035 
20036  /* check to see if we received an error getting the domain */
20037  if (pt_has_error (parser))
20038  {
20039  if (result)
20040  {
20041  if (result != expr)
20042  {
20043  parser_free_tree (parser, result);
20044  }
20045  }
20046 
20047  has_error = true;
20048  was_error_set = true;
20049  goto end;
20050  }
20051 
20052  if (!pt_check_const_fold_op_w_args (op, arg1, arg2, arg3, domain))
20053  {
20054  goto end;
20055  }
20056 
20057  if (pt_evaluate_db_value_expr (parser, expr, op, arg1, arg2, arg3, &dbval_res, domain, opd1, opd2, opd3,
20058  qualifier))
20059  {
20060  result = pt_dbval_to_value (parser, &dbval_res);
20061  if (result->data_type == NULL && result->type_enum != PT_TYPE_NULL)
20062  {
20063  /* data_type may be needed later... e.g. in CTEs */
20064  result->data_type = parser_copy_tree_list (parser, expr->data_type);
20065  }
20066  if (result && expr->or_next && result != expr)
20067  {
20068  PT_NODE *other;
20069  DB_VALUE *db_value;
20070 
20071  /* i.e., op == PT_OR */
20072  db_value = pt_value_to_db (parser, result); /* opd1 */
20073  other = expr->or_next; /* opd2 */
20075  {
20076  result = other;
20077  }
20079  {
20080  parser_free_tree (parser, result->or_next);
20081  result->or_next = NULL;
20082  }
20083  else
20084  {
20085  result = other;
20086  }
20087 
20088  result = parser_copy_tree_list (parser, result);
20089  if (result == NULL)
20090  {
20091  pr_clear_value (&dbval_res);
20092  has_error = true;
20093  goto end;
20094  }
20095  }
20096  }
20097  }
20098  else if (result_type == PT_TYPE_LOGICAL)
20099  {
20100  /* We'll check to see if the expression is always true or false due to type boundary overflows */
20101  if (opd1 && opd1->node_type == PT_VALUE && opd2 && opd2->node_type == PT_VALUE)
20102  {
20103  result = pt_compare_bounds_to_value (parser, expr, op, opd1->type_enum, arg2, type2);
20104  }
20105 
20106  if (result && expr->or_next && result != expr)
20107  {
20108  PT_NODE *other;
20109  DB_VALUE *db_value;
20110 
20111  /* i.e., op == PT_OR */
20112  db_value = pt_value_to_db (parser, result); /* opd1 */
20113  other = result->or_next; /* opd2 */
20115  {
20116  result = other;
20117  }
20119  {
20120  parser_free_tree (parser, result->or_next);
20121  result->or_next = NULL;
20122  }
20123  else
20124  {
20125  result = other;
20126  }
20127 
20128  result = parser_copy_tree_list (parser, result);
20129  }
20130 
20131 #if 0
20132  /* We tried to fold trivial expressions which is always true: e.g, inst_num() > 0
20133  * This looks like a nice optimization but it causes defects with rewrite optimization of limit clause.
20134  * Once we fold a rewritten predicate here, limit clause might be bound with an incorrect hostvar.
20135  * Please note that limit clause and rewritten predicates works independently.
20136  * The optimization cannot be applied until we change the design of limit evaluation.
20137  */
20138  if (opd1 && opd1->node_type == PT_EXPR && opd2 && opd2->node_type == PT_VALUE
20139  && (opd1->info.expr.op == PT_INST_NUM || opd1->info.expr.op == PT_ORDERBY_NUM)
20140  && (opd2->type_enum == PT_TYPE_INTEGER || opd2->type_enum == PT_TYPE_BIGINT))
20141  {
20142  DB_BIGINT rvalue;
20143 
20144  if (opd2->type_enum == PT_TYPE_INTEGER)
20145  {
20146  rvalue = opd2->info.value.data_value.i;
20147  }
20148  else if (opd2->type_enum == PT_TYPE_BIGINT)
20149  {
20150  rvalue = opd2->info.value.data_value.bigint;
20151  }
20152  else
20153  {
20154  assert (0);
20155  rvalue = 0;
20156  }
20157 
20158  if ((op == PT_GT && rvalue <= 0) || (op == PT_GE && rvalue <= 1))
20159  {
20160  /* always true */
20161  db_make_int (&dbval_res, 1);
20162  result = pt_dbval_to_value (parser, &dbval_res);
20163  }
20164  }
20165 #endif
20166 
20167  if (result == NULL)
20168  {
20169  has_error = true;
20170  goto end;
20171  }
20172  }
20173 
20174 end:
20175  pr_clear_value (&dbval_res);
20176 
20177  if (has_error)
20178  {
20179  if (!was_error_set)
20180  {
20181  PT_ERRORc (parser, expr, er_msg ());
20182  }
20183 
20184  expr->next = expr_next;
20185  return expr;
20186  }
20187 
20188  if (result)
20189  {
20190  result->line_number = line;
20191  result->column_number = column;
20192  result->alias_print = alias_print;
20193  result->is_hidden_column = is_hidden_column;
20194  result->is_value_query = expr->is_value_query;
20195 
20196  if (result != expr)
20197  {
20198  if (alias_print == NULL || (PT_IS_VALUE_NODE (result) && !result->info.value.text))
20199  {
20200  /* print expr to alias_print */
20201  expr->alias_print = NULL;
20203  }
20204  if (alias_print == NULL)
20205  {
20206  alias_print = expr->alias_print;
20207  }
20208  if (result->alias_print == NULL && expr->is_alias_enabled_expr)
20209  {
20210  result->alias_print = pt_append_string (parser, NULL, alias_print);
20211  }
20212  if (PT_IS_VALUE_NODE (result) && !result->info.value.text)
20213  {
20214  result->info.value.text = pt_append_string (parser, NULL, expr->alias_print);
20215  }
20216  if (PT_IS_VALUE_NODE (result))
20217  {
20218  result->info.value.is_collate_allowed = true;
20219  }
20220  parser_free_tree (parser, expr);
20221  }
20222 
20223  result->next = expr_next;
20224 
20225  if (result->type_enum != PT_TYPE_NA && result->type_enum != PT_TYPE_NULL)
20226  {
20227  result->type_enum = result_type;
20228  }
20229 
20230  if (result->node_type == PT_EXPR)
20231  {
20232  result->info.expr.location = location;
20233  }
20234  else if (result->node_type == PT_VALUE)
20235  {
20236  result->info.value.location = location;
20237  }
20238 
20239  return result;
20240  }
20241  else
20242  {
20243  expr->next = expr_next;
20244  return expr;
20245  }
20246 }
20247 
20248 /*
20249  * pt_evaluate_function_w_args () - evaluate the function to a DB_VALUE
20250  * return: 1, if successful,
20251  * 0, if not successful.
20252  * parser(in): parser global context info for reentrancy
20253  * fcode(in): function code
20254  * args(in): array of arguments' values
20255  * num_args(in): number of arguments
20256  * result(out): result value of function (if evaluated)
20257  */
20258 int
20260  DB_VALUE * result)
20261 {
20262  int error = NO_ERROR, i;
20263 
20264  assert (parser != NULL);
20265  assert (args != NULL);
20266  assert (result != NULL);
20267 
20268  if (!args || !result)
20269  {
20270  return 0;
20271  }
20272 
20273  /* init array vars */
20274  for (i = 0; i < num_args; i++)
20275  {
20276  assert (args[i] != NULL);
20277  }
20278 
20279  switch (fcode)
20280  {
20281  case F_INSERT_SUBSTRING:
20282  error = db_string_insert_substring (args[0], args[1], args[2], args[3], result);
20283  if (error != NO_ERROR)
20284  {
20285  return 0;
20286  }
20287  break;
20288  case F_ELT:
20289  error = db_string_elt (result, args, num_args);
20290  if (error != NO_ERROR)
20291  {
20292  return 0;
20293  }
20294  break;
20295  case F_JSON_OBJECT:
20296  error = db_json_object (result, args, num_args);
20297  if (error != NO_ERROR)
20298  {
20299  PT_ERRORc (parser, NULL, er_msg ());
20300  return 0;
20301  }
20302  break;
20303  case F_JSON_ARRAY:
20304  error = db_json_array (result, args, num_args);
20305  if (error != NO_ERROR)
20306  {
20307  PT_ERRORc (parser, NULL, er_msg ());
20308  return 0;
20309  }
20310  break;
20311  case F_JSON_INSERT:
20312  error = db_json_insert (result, args, num_args);
20313  if (error != NO_ERROR)
20314  {
20315  PT_ERRORc (parser, NULL, er_msg ());
20316  return 0;
20317  }
20318  break;
20319  case F_JSON_REPLACE:
20320  error = db_json_replace (result, args, num_args);
20321  if (error != NO_ERROR)
20322  {
20323  PT_ERRORc (parser, NULL, er_msg ());
20324  return 0;
20325  }
20326  break;
20327  case F_JSON_SET:
20328  error = db_json_set (result, args, num_args);
20329  if (error != NO_ERROR)
20330  {
20331  PT_ERRORc (parser, NULL, er_msg ());
20332  return 0;
20333  }
20334  break;
20335  case F_JSON_KEYS:
20336  error = db_json_keys (result, args, num_args);
20337  if (error != NO_ERROR)
20338  {
20339  PT_ERRORc (parser, NULL, er_msg ());
20340  return 0;
20341  }
20342  break;
20343  case F_JSON_REMOVE:
20344  error = db_json_remove (result, args, num_args);
20345  if (error != NO_ERROR)
20346  {
20347  PT_ERRORc (parser, NULL, er_msg ());
20348  return 0;
20349  }
20350  break;
20351  case F_JSON_ARRAY_APPEND:
20352  error = db_json_array_append (result, args, num_args);
20353  if (error != NO_ERROR)
20354  {
20355  PT_ERRORc (parser, NULL, er_msg ());
20356  return 0;
20357  }
20358  break;
20359  case F_JSON_MERGE:
20360  error = db_json_merge (result, args, num_args);
20361  if (error != NO_ERROR)
20362  {
20363  PT_ERRORc (parser, NULL, er_msg ());
20364  return 0;
20365  }
20366  break;
20367  case F_JSON_GET_ALL_PATHS:
20368  error = db_json_get_all_paths (result, args, num_args);
20369  if (error != NO_ERROR)
20370  {
20371  PT_ERRORc (parser, NULL, er_msg ());
20372  return 0;
20373  }
20374  break;
20375  default:
20376  /* a supported function doesn't have const folding code */
20377  assert (false);
20378  break;
20379  }
20380  return 1;
20381 }
20382 
20383 /*
20384  * pt_fold_const_function () - evaluate constant function
20385  * return: the evaluated expression, if successful,
20386  * unchanged function, if not successful.
20387  * parser(in): parser global context info for reentrancy
20388  * func(in): a parse tree representation of a possibly constant function
20389  */
20390 static PT_NODE *
20391 pt_fold_const_function (PARSER_CONTEXT * parser, PT_NODE * func)
20392 {
20393  PT_TYPE_ENUM result_type = PT_TYPE_NONE;
20394  PT_NODE *result = NULL;
20395  DB_VALUE dbval_res;
20396  PT_NODE *func_next;
20397  int line = 0, column = 0;
20398  short location;
20399  const char *alias_print = NULL;
20400 
20401  if (func == NULL)
20402  {
20403  return func;
20404  }
20405 
20406  if (func->node_type != PT_FUNCTION)
20407  {
20408  return func;
20409  }
20410 
20411  if (func->do_not_fold)
20412  {
20413  return func;
20414  }
20415 
20416  /* only functions wrapped with expressions are supported */
20417  if (!pt_is_expr_wrapped_function (parser, func))
20418  {
20419  return func;
20420  }
20421 
20422 
20423  /* PT_FUNCTION doesn't have location attribute as PT_EXPR does temporary set location to 0 ( WHERE clause) */
20424  location = 0;
20425 
20426  db_make_null (&dbval_res);
20427 
20428  line = func->line_number;
20429  column = func->column_number;
20430  alias_print = func->alias_print;
20431  func_next = func->next;
20432  func->next = NULL;
20433  result_type = func->type_enum;
20434  result = func;
20435 
20436  if (pt_evaluate_function (parser, func, &dbval_res) == NO_ERROR)
20437  {
20438  result = pt_dbval_to_value (parser, &dbval_res);
20439  }
20440 
20441  pr_clear_value (&dbval_res);
20442 
20443  if (result)
20444  {
20445  if (result != func)
20446  {
20447  if (alias_print == NULL || (PT_IS_VALUE_NODE (result) && !result->info.value.text))
20448  {
20449  func->alias_print = NULL;
20451  }
20452  if (PT_IS_VALUE_NODE (result) && !result->info.value.text)
20453  {
20454  result->info.value.text = func->alias_print;
20455  }
20456  if (alias_print == NULL && func->is_alias_enabled_expr)
20457  {
20458  alias_print = func->alias_print;
20459  }
20460 
20461  if (PT_IS_VALUE_NODE (result))
20462  {
20463  result->info.value.is_collate_allowed = true;
20464  }
20465 
20466  parser_free_tree (parser, func);
20467  }
20468 
20469  /* restore saved func attributes */
20470  result->next = func_next;
20471 
20472  if (result->type_enum != PT_TYPE_NA && result->type_enum != PT_TYPE_NULL)
20473  {
20474  result->type_enum = result_type;
20475  }
20476 
20477  result->line_number = line;
20478  result->column_number = column;
20479  result->alias_print = alias_print;
20480  if (result->node_type == PT_FUNCTION)
20481  {
20482  assert (result->info.function.arg_list != NULL);
20483  }
20484  else if (result->node_type == PT_VALUE)
20485  {
20486  /* temporary set location to a 0 the location will be updated after const folding at the upper level : the
20487  * parent node is a PT_EXPR node with a PT_FUNCTION_HOLDER operator type */
20488  result->info.value.location = location;
20489  }
20490  }
20491 
20492  return result;
20493 } /* pt_fold_const_function() */
20494 
20495 /*
20496  * pt_evaluate_function () - evaluate constant function
20497  * return: NO_ERROR, if evaluation successfull,
20498  * an error code, if unsuccessful
20499  * parser(in): parser global context info for reentrancy
20500  * func(in): a parse tree representation of a possibly constant function
20501  * dbval_res(in/out): the result DB_VALUE of evaluation
20502  */
20503 int
20505 {
20506  PT_NODE *operand;
20507  DB_VALUE dummy, **arg_array;
20508  FUNC_TYPE fcode;
20509  int error = NO_ERROR, i;
20510  int num_args = 0;
20511  bool all_args_const = false;
20512 
20513  /* init array variables */
20514  arg_array = NULL;
20515 
20516  if (func == NULL)
20517  {
20518  return ER_FAILED;
20519  }
20520 
20521  if (func->node_type != PT_FUNCTION)
20522  {
20523  return ER_FAILED;
20524  }
20525 
20526  fcode = func->info.function.function_type;
20527  /* only functions wrapped with expressions are supported */
20528  if (!pt_is_expr_wrapped_function (parser, func))
20529  {
20530  return ER_FAILED;
20531  }
20532 
20533  db_make_null (dbval_res);
20534 
20535  /* count function's arguments */
20536  operand = func->info.function.arg_list;
20537  num_args = 0;
20538  while (operand)
20539  {
20540  ++num_args;
20541  operand = operand->next;
20542  }
20543  assert (num_args > 0);
20544 
20545  arg_array = (DB_VALUE **) calloc (num_args, sizeof (DB_VALUE *));
20546  if (arg_array == NULL)
20547  {
20548  goto end;
20549  }
20550 
20551  /* convert all operands to DB_VALUE arguments */
20552  /* for some functions this may not be necessary : you need to break from this loop and solve them at next steps */
20553  all_args_const = true;
20554  operand = func->info.function.arg_list;
20555  for (i = 0; i < num_args; i++)
20556  {
20557  if (operand != NULL && operand->node_type == PT_VALUE)
20558  {
20559  DB_VALUE *arg = NULL;
20560 
20561  arg = pt_value_to_db (parser, operand);
20562  if (arg == NULL)
20563  {
20564  all_args_const = false;
20565  break;
20566  }
20567  else
20568  {
20569  arg_array[i] = arg;
20570  }
20571  }
20572  else
20573  {
20574  db_make_null (&dummy);
20575  arg_array[i] = &dummy;
20576  all_args_const = false;
20577  break;
20578  }
20579  operand = operand->next;
20580  }
20581 
20582  if (all_args_const && i == num_args)
20583  {
20584  TP_DOMAIN *domain;
20585 
20586  /* use the caching variant of this function ! */
20587  domain = pt_xasl_node_to_domain (parser, func);
20588 
20589  /* check if we received an error getting the domain */
20590  if (pt_has_error (parser))
20591  {
20592  pr_clear_value (dbval_res);
20593  error = ER_FAILED;
20594  goto end;
20595  }
20596 
20597  if (pt_evaluate_function_w_args (parser, fcode, arg_array, num_args, dbval_res) != 1)
20598  {
20599  error = ER_FAILED;
20600  goto end;
20601  }
20602  }
20603  else
20604  {
20605  error = ER_FAILED;
20606  }
20607 end:
20608  if (arg_array != NULL)
20609  {
20610  free_and_init (arg_array);
20611  }
20612 
20613  return error;
20614 }
20615 
20616 /*
20617  * pt_semantic_type () - sets data types for all expressions in a parse tree
20618  * and evaluates constant sub expressions
20619  * return:
20620  * parser(in):
20621  * tree(in/out):
20622  * sc_info_ptr(in):
20623  */
20624 
20625 PT_NODE *
20627 {
20628  SEMANTIC_CHK_INFO sc_info = { NULL, NULL, 0, 0, 0, false, false };
20629 
20630  if (pt_has_error (parser))
20631  {
20632  return NULL;
20633  }
20634 
20635  if (sc_info_ptr)
20636  {
20637  /* do type checking */
20638  tree = parser_walk_tree (parser, tree, pt_eval_type_pre, sc_info_ptr, pt_eval_type, sc_info_ptr);
20639 
20640  /* do constant folding */
20641  tree = parser_walk_tree (parser, tree, NULL, NULL, pt_fold_constants, sc_info_ptr);
20642  }
20643  else
20644  {
20645  sc_info.top_node = tree;
20646  sc_info.donot_fold = false;
20647  /* do type checking */
20648  tree = parser_walk_tree (parser, tree, pt_eval_type_pre, &sc_info, pt_eval_type, &sc_info);
20649 
20650  /* do constant folding */
20651  tree = parser_walk_tree (parser, tree, NULL, NULL, pt_fold_constants, &sc_info);
20652  }
20653 
20654  if (pt_has_error (parser))
20655  {
20656  tree = NULL;
20657  }
20658 
20659  return tree;
20660 }
20661 
20662 
20663 /*
20664  * pt_class_name () - return the class name of a data_type node
20665  * return:
20666  * type(in): a data_type node
20667  */
20668 static const char *
20669 pt_class_name (const PT_NODE * type)
20670 {
20671  if (!type || type->node_type != PT_DATA_TYPE || !type->info.data_type.entity
20672  || type->info.data_type.entity->node_type != PT_NAME)
20673  {
20674  return NULL;
20675  }
20676  else
20677  {
20678  return type->info.data_type.entity->info.name.original;
20679  }
20680 }
20681 
20682 /*
20683  * pt_set_default_data_type () -
20684  * return:
20685  * parser(in):
20686  * type(in):
20687  * dtp(in):
20688  */
20689 static int
20690 pt_set_default_data_type (PARSER_CONTEXT * parser, PT_TYPE_ENUM type, PT_NODE ** dtp)
20691 {
20692  PT_NODE *dt;
20693  int error = NO_ERROR;
20694 
20696  if (dt == NULL)
20697  {
20698  return ER_GENERIC_ERROR;
20699  }
20700 
20701  dt->type_enum = type;
20702  switch (type)
20703  {
20704  case PT_TYPE_CHAR:
20705  case PT_TYPE_VARCHAR:
20709  break;
20710 
20711  case PT_TYPE_NCHAR:
20712  case PT_TYPE_VARNCHAR:
20716  break;
20717 
20718  case PT_TYPE_BIT:
20719  case PT_TYPE_VARBIT:
20722  break;
20723 
20724  case PT_TYPE_NUMERIC:
20726  /*
20727  * FIX ME!! Is it the case that this will always happen in
20728  * zero-scale context? That's certainly the case when we're
20729  * coercing from integers, but what about floats and doubles?
20730  */
20731  dt->info.data_type.dec_precision = 0;
20732  break;
20733 
20734  default:
20735  PT_INTERNAL_ERROR (parser, "type check");
20737  break;
20738  }
20739 
20740  *dtp = dt;
20741  return error;
20742 }
20743 
20744 /*
20745  * pt_is_explicit_coerce_allowed_for_default_value () - check whether explicit coercion is allowed for default value
20746  * return: true, if explicit coercion is allowed
20747  * parser(in): parser context
20748  * data_type(in): data type to coerce
20749  * desired_type(in): desired type
20750  */
20751 static bool
20752 pt_is_explicit_coerce_allowed_for_default_value (PARSER_CONTEXT * parser, PT_TYPE_ENUM data_type,
20753  PT_TYPE_ENUM desired_type)
20754 {
20755  /* Complete this function with other types that allow explicit coerce for default value */
20756  if (PT_IS_NUMERIC_TYPE (data_type))
20757  {
20758  if (PT_IS_STRING_TYPE (desired_type))
20759  {
20760  /* We allow explicit coerce from integer to string types. */
20761  return true;
20762  }
20763  }
20764 
20765  return false;
20766 }
20767 
20768 /*
20769  * pt_coerce_value () - coerce a PT_VALUE into another PT_VALUE of compatible type
20770  * return: NO_ERROR on success, non-zero for ERROR
20771  * parser(in):
20772  * src(in): a pointer to the original PT_VALUE
20773  * dest(out): a pointer to the coerced PT_VALUE
20774  * desired_type(in): the desired type of the coerced result
20775  * data_type(in): the data type list of a (desired) set type or the data type of an object or NULL
20776  */
20777 int
20779 {
20780  return pt_coerce_value_internal (parser, src, dest, desired_type, data_type, false, true);
20781 }
20782 
20783 /*
20784  * pt_coerce_value_for_default_value () - coerce a PT_VALUE of DEFAULT into another PT_VALUE of compatible type
20785  * return: NO_ERROR on success, non-zero for ERROR
20786  * parser(in):
20787  * src(in): a pointer to the original PT_VALUE
20788  * dest(out): a pointer to the coerced PT_VALUE
20789  * desired_type(in): the desired type of the coerced result
20790  * data_type(in): the data type list of a (desired) set type or the data type of an object or NULL
20791  * default_expr_type(in): default expression identifier
20792  */
20793 int
20795  PT_NODE * data_type, DB_DEFAULT_EXPR_TYPE default_expr_type)
20796 {
20797  bool implicit_coercion;
20798 
20799  assert (src != NULL && dest != NULL);
20800 
20801  if (default_expr_type == DB_DEFAULT_NONE && src->node_type == PT_VALUE
20802  && pt_is_explicit_coerce_allowed_for_default_value (parser, src->type_enum, desired_type))
20803  {
20804  implicit_coercion = false; /* explicit coercion */
20805  }
20806  else
20807  {
20808  implicit_coercion = true;
20809  }
20810 
20811  return pt_coerce_value_internal (parser, src, dest, desired_type, data_type, true, implicit_coercion);
20812 }
20813 
20814 /*
20815  * pt_coerce_value_internal () - coerce a PT_VALUE into another PT_VALUE of compatible type
20816  * return: NO_ERROR on success, non-zero for ERROR
20817  * parser(in):
20818  * src(in): a pointer to the original PT_VALUE
20819  * dest(out): a pointer to the coerced PT_VALUE
20820  * desired_type(in): the desired type of the coerced result
20821  * data_type(in): the data type list of a (desired) set type or the data type of an object or NULL
20822  * check_string_precision(in): true, if needs to consider string precision
20823  * do_implicit_coercion(in): true for implicit coercion, false for explicit coercion
20824  */
20825 static int
20826 pt_coerce_value_internal (PARSER_CONTEXT * parser, PT_NODE * src, PT_NODE * dest, PT_TYPE_ENUM desired_type,
20827  PT_NODE * data_type, bool check_string_precision, bool implicit_coercion)
20828 {
20829  PT_TYPE_ENUM original_type;
20830  PT_NODE *dest_next;
20831  int err = NO_ERROR;
20832  PT_NODE *temp = NULL;
20833  bool is_collation_change = false;
20834  bool has_type_string;
20835  bool is_same_type;
20836  DB_VALUE *db_src = NULL;
20837  bool need_src_clear = false;
20838 
20839  assert (src != NULL && dest != NULL);
20840 
20841  dest_next = dest->next;
20842 
20843  original_type = src->type_enum;
20844 
20845  if (PT_HAS_COLLATION (original_type) && PT_HAS_COLLATION (desired_type) && src->data_type != NULL && data_type != NULL
20847  {
20848  is_collation_change = true;
20849  }
20850 
20851  has_type_string = PT_IS_STRING_TYPE (original_type);
20852  is_same_type = (original_type == desired_type && !is_collation_change);
20853 
20854  if ((is_same_type && original_type != PT_TYPE_NUMERIC && desired_type != PT_TYPE_OBJECT
20855  && (has_type_string == false || check_string_precision == false))
20856  || original_type == PT_TYPE_NA || original_type == PT_TYPE_NULL)
20857  {
20858  if (src != dest)
20859  {
20860  *dest = *src;
20861  dest->next = dest_next;
20862  }
20863  return NO_ERROR;
20864  }
20865 
20866  if (data_type == NULL && PT_IS_PARAMETERIZED_TYPE (desired_type)
20867  && (err = pt_set_default_data_type (parser, desired_type, &data_type) < 0))
20868  {
20869  return err;
20870  }
20871 
20872  if (is_same_type && src->data_type != NULL)
20873  {
20874  if ((original_type == PT_TYPE_NUMERIC
20875  && (src->data_type->info.data_type.precision == data_type->info.data_type.precision)
20877  || (has_type_string && src->data_type->info.data_type.precision == data_type->info.data_type.precision))
20878  {
20879  /* match */
20880  if (src != dest)
20881  {
20882  *dest = *src;
20883  dest->next = dest_next;
20884  }
20885  return NO_ERROR;
20886  }
20887  }
20888 
20889  if (original_type == PT_TYPE_NONE && src->node_type != PT_HOST_VAR)
20890  {
20891  if (src != dest)
20892  {
20893  *dest = *src;
20894  dest->next = dest_next;
20895  }
20896  dest->type_enum = desired_type;
20897  dest->data_type = parser_copy_tree_list (parser, data_type);
20898  /* don't return, in case further coercion is needed set original type to match desired type to avoid confusing
20899  * type check below */
20900  }
20901 
20902  switch (src->node_type)
20903  {
20904  case PT_HOST_VAR:
20905  /* binding of host variables may be delayed in the case of an esql PREPARE statement until an OPEN cursor or an
20906  * EXECUTE statement. in this case we seem to have no choice but to assume each host variable is typeless and can
20907  * be coerced into any desired type. */
20908  if (parser->set_host_var == 0)
20909  {
20910  dest->type_enum = desired_type;
20911  dest->data_type = parser_copy_tree_list (parser, data_type);
20912  return NO_ERROR;
20913  }
20914 
20915  /* otherwise fall through to the PT_VALUE case */
20916 
20917  case PT_VALUE:
20918  {
20919  DB_VALUE db_dest;
20920  TP_DOMAIN *desired_domain;
20921 
20922  db_src = pt_value_to_db (parser, src);
20923  if (!db_src)
20924  {
20926  break;
20927  }
20928 
20929  db_make_null (&db_dest);
20930 
20931  /* be sure to use the domain caching versions */
20932  if (data_type)
20933  {
20934  desired_domain = pt_node_data_type_to_db_domain (parser, (PT_NODE *) data_type, desired_type);
20935  /* need a caching version of this function ? */
20936  if (desired_domain != NULL)
20937  {
20938  desired_domain = tp_domain_cache (desired_domain);
20939  }
20940  }
20941  else
20942  {
20943  desired_domain = pt_xasl_type_enum_to_domain (desired_type);
20944  }
20945 
20946  err = tp_value_cast (db_src, &db_dest, desired_domain, implicit_coercion);
20947 
20948  switch (err)
20949  {
20950  case DOMAIN_INCOMPATIBLE:
20952  break;
20953  case DOMAIN_OVERFLOW:
20955  break;
20956  case DOMAIN_ERROR:
20957  assert (er_errid () != NO_ERROR);
20958  err = er_errid ();
20959  break;
20960  default:
20961  break;
20962  }
20963 
20964  if (err == DOMAIN_COMPATIBLE && src->node_type == PT_HOST_VAR
20966  {
20967  /* when the type of the host variable is compatible to coerce, it is enough. NEVER change the node type to
20968  * PT_VALUE. */
20969  pr_clear_value (&db_dest);
20970  return NO_ERROR;
20971  }
20972 
20974  {
20975  if (err == NO_ERROR)
20976  {
20977  (void) pr_clear_value (db_src);
20978  }
20979  else
20980  {
20981  /* still needs db_src to print the message */
20982  need_src_clear = true;
20983  }
20984  }
20985 
20986  if (err >= 0)
20987  {
20988  temp = pt_dbval_to_value (parser, &db_dest);
20989  (void) pr_clear_value (&db_dest);
20990  if (!temp)
20991  {
20993  }
20994  else
20995  {
20996  temp->line_number = dest->line_number;
20997  temp->column_number = dest->column_number;
20998  temp->alias_print = dest->alias_print;
20999  temp->info.value.print_charset = dest->info.value.print_charset;
21000  temp->info.value.print_collation = dest->info.value.print_collation;
21001  temp->info.value.is_collate_allowed = dest->info.value.is_collate_allowed;
21002  *dest = *temp;
21003  if (data_type != NULL)
21004  {
21005  dest->data_type = parser_copy_tree_list (parser, data_type);
21006  if (dest->data_type == NULL)
21007  {
21009  }
21010  }
21011  dest->next = dest_next;
21013  parser_free_node (parser, temp);
21014  }
21015  }
21016  }
21017  break;
21018 
21019  case PT_FUNCTION:
21020  if (src == dest)
21021  {
21022  switch (src->info.function.function_type)
21023  {
21024  case F_MULTISET:
21025  case F_SEQUENCE:
21026  switch (desired_type)
21027  {
21028  case PT_TYPE_SET:
21029  dest->info.function.function_type = F_SET;
21030  dest->type_enum = PT_TYPE_SET;
21031  break;
21032  case PT_TYPE_SEQUENCE:
21033  dest->info.function.function_type = F_SEQUENCE;
21034  dest->type_enum = PT_TYPE_SEQUENCE;
21035  break;
21036  case PT_TYPE_MULTISET:
21037  dest->info.function.function_type = F_MULTISET;
21038  dest->type_enum = PT_TYPE_MULTISET;
21039  break;
21040  default:
21041  break;
21042  }
21043  break;
21044 
21045  case F_TABLE_MULTISET:
21046  case F_TABLE_SEQUENCE:
21047  switch (desired_type)
21048  {
21049  case PT_TYPE_SET:
21050  dest->info.function.function_type = F_TABLE_SET;
21051  dest->type_enum = PT_TYPE_SET;
21052  break;
21053  case PT_TYPE_SEQUENCE:
21054  dest->info.function.function_type = F_TABLE_SEQUENCE;
21055  dest->type_enum = PT_TYPE_SEQUENCE;
21056  break;
21057  case PT_TYPE_MULTISET:
21058  dest->info.function.function_type = F_TABLE_MULTISET;
21059  dest->type_enum = PT_TYPE_MULTISET;
21060  break;
21061  default:
21062  break;
21063  }
21064  break;
21065 
21066  default:
21067  break;
21068  }
21069  }
21070  break;
21071 
21072  default:
21074  break;
21075  }
21076 
21077  if (err == ER_IT_DATA_OVERFLOW)
21078  {
21080  pt_short_print (parser, src), pt_show_type_enum (desired_type));
21081  }
21082  else if (err < 0)
21083  {
21085  pt_short_print (parser, src),
21086  (desired_type == PT_TYPE_OBJECT ? pt_class_name (data_type) : pt_show_type_enum (desired_type)));
21087  }
21088 
21089  if (need_src_clear)
21090  {
21091  (void) pr_clear_value (db_src);
21092  }
21093 
21094  return err;
21095 }
21096 
21097 #if defined(ENABLE_UNUSED_FUNCTION)
21098 /*
21099  * generic_func_casecmp () -
21100  * return:
21101  * a(in):
21102  * b(in):
21103  */
21104 static int
21105 generic_func_casecmp (const void *a, const void *b)
21106 {
21107  return intl_identifier_casecmp (((const GENERIC_FUNCTION_RECORD *) a)->function_name,
21108  ((const GENERIC_FUNCTION_RECORD *) b)->function_name);
21109 }
21110 
21111 /*
21112  * init_generic_funcs () -
21113  * return:
21114  * void(in):
21115  */
21116 static void
21117 init_generic_funcs (void)
21118 {
21119  qsort (pt_Generic_functions, (sizeof (pt_Generic_functions) / sizeof (GENERIC_FUNCTION_RECORD)),
21120  sizeof (GENERIC_FUNCTION_RECORD), &generic_func_casecmp);
21121  pt_Generic_functions_sorted = 1;
21122 }
21123 #endif /* ENABLE_UNUSED_FUNCTION */
21124 
21125 /*
21126  * pt_type_generic_func () - Searches the generic_funcs table to find
21127  * the given generic function
21128  * return: 1 if the generic function is defined, 0 otherwise
21129  * parser(in):
21130  * node(in):
21131  */
21132 
21133 int
21135 {
21136 #if !defined(ENABLE_UNUSED_FUNCTION)
21137  /* If you want to use generic function, remove this block. */
21138  return 0;
21139 #else /* !ENABLE_UNUSED_FUNCTION */
21140  GENERIC_FUNCTION_RECORD *record_p, key;
21141  PT_NODE *offset;
21142 
21143  if (!pt_Generic_functions_sorted)
21144  {
21145  init_generic_funcs ();
21146  }
21147 
21148  if (node->node_type != PT_FUNCTION || node->info.function.function_type != PT_GENERIC
21149  || !node->info.function.generic_name)
21150  {
21151  return 0; /* this is not a generic function */
21152  }
21153 
21154  /* Check first to see if the function exists in our table. */
21155  key.function_name = node->info.function.generic_name;
21156  record_p =
21157  (GENERIC_FUNCTION_RECORD *) bsearch (&key, pt_Generic_functions,
21158  (sizeof (pt_Generic_functions) / sizeof (GENERIC_FUNCTION_RECORD)),
21159  sizeof (GENERIC_FUNCTION_RECORD), &generic_func_casecmp);
21160  if (!record_p)
21161  {
21162  return 0; /* we can't find it */
21163  }
21164 
21166  if (offset == NULL)
21167  {
21168  return 0;
21169  }
21170 
21171  offset->type_enum = PT_TYPE_INTEGER;
21172  offset->info.value.data_value.i = record_p->func_ptr_offset;
21173  node->info.function.arg_list = parser_append_node (node->info.function.arg_list, offset);
21174 
21175  /* type the node */
21176  pt_string_to_data_type (parser, record_p->return_type, node);
21177 
21178  return 1;
21179 #endif /* ENABLE_UNUSED_FUNCTION */
21180 }
21181 
21182 
21183 /*
21184  * pt_compare_bounds_to_value () - compare constant value to base type
21185  * boundaries. If value is out of bounds, we already know the
21186  * result of a logical comparison (<, >, <=, >=, ==)
21187  * return: null or logical value node set to true or false.
21188  * parser(in): parse tree
21189  * expr(in): logical expression to be examined
21190  * op(in): expression operator
21191  * lhs_type(in): type of left hand operand
21192  * rhs_val(in): value of right hand operand
21193  * rhs_type(in): type of right hand operand
21194  *
21195  * Note :
21196  * This function coerces a PT_VALUE to another PT_VALUE of compatible type.
21197  */
21198 
21199 static PT_NODE *
21200 pt_compare_bounds_to_value (PARSER_CONTEXT * parser, PT_NODE * expr, PT_OP_TYPE op, PT_TYPE_ENUM lhs_type,
21201  DB_VALUE * rhs_val, PT_TYPE_ENUM rhs_type)
21202 {
21203  bool lhs_less = false;
21204  bool lhs_greater = false;
21205  bool always_false = false;
21206  bool always_false_due_to_null = false;
21207  bool always_true = false;
21208  PT_NODE *result = expr;
21209  double dtmp;
21210 
21211  /* we can't determine anything if the types are the same */
21212  if (lhs_type == rhs_type)
21213  {
21214  return result;
21215  }
21216 
21217  /* check if op is always false due to null */
21218  if (op != PT_IS_NULL && op != PT_IS_NOT_NULL)
21219  {
21220  if (DB_IS_NULL (rhs_val) && rhs_type != PT_TYPE_SET && rhs_type != PT_TYPE_SEQUENCE
21221  && rhs_type != PT_TYPE_MULTISET)
21222  {
21223  always_false_due_to_null = true;
21224  goto end;
21225  }
21226  }
21227 
21228  /* we only allow PT_EQ, PT_GT, PT_GE, PT_LT, PT_LE. */
21229  if (op != PT_EQ && op != PT_GT && op != PT_GE && op != PT_LT && op != PT_LE)
21230  {
21231  return result;
21232  }
21233 
21234  /* we need to extend the following to compare dates and times, but probably not until we make the ranges of PT_* and
21235  * DB_* the same */
21236  switch (lhs_type)
21237  {
21238  case PT_TYPE_SMALLINT:
21239  switch (rhs_type)
21240  {
21241  case PT_TYPE_INTEGER:
21242  if (db_get_int (rhs_val) > DB_INT16_MAX)
21243  lhs_less = true;
21244  else if (db_get_int (rhs_val) < DB_INT16_MIN)
21245  lhs_greater = true;
21246  break;
21247  case PT_TYPE_BIGINT:
21248  if (db_get_bigint (rhs_val) > DB_INT16_MAX)
21249  lhs_less = true;
21250  else if (db_get_bigint (rhs_val) < DB_INT16_MIN)
21251  lhs_greater = true;
21252  break;
21253  case PT_TYPE_FLOAT:
21254  if (db_get_float (rhs_val) > DB_INT16_MAX)
21255  lhs_less = true;
21256  else if (db_get_float (rhs_val) < DB_INT16_MIN)
21257  lhs_greater = true;
21258  break;
21259 
21260  case PT_TYPE_DOUBLE:
21261  if (db_get_double (rhs_val) > DB_INT16_MAX)
21262  lhs_less = true;
21263  else if (db_get_double (rhs_val) < DB_INT16_MIN)
21264  lhs_greater = true;
21265  break;
21266 
21267  case PT_TYPE_NUMERIC:
21268  numeric_coerce_num_to_double (db_locate_numeric (rhs_val), DB_VALUE_SCALE (rhs_val), &dtmp);
21269  if (dtmp > DB_INT16_MAX)
21270  lhs_less = true;
21271  else if (dtmp < DB_INT16_MIN)
21272  lhs_greater = true;
21273  break;
21274 
21275  case PT_TYPE_MONETARY:
21276  dtmp = (db_get_monetary (rhs_val))->amount;
21277  if (dtmp > DB_INT16_MAX)
21278  lhs_less = true;
21279  else if (dtmp < DB_INT16_MIN)
21280  lhs_greater = true;
21281  break;
21282 
21283  default:
21284  break;
21285  }
21286  break;
21287 
21288  case PT_TYPE_INTEGER:
21289  switch (rhs_type)
21290  {
21291  case PT_TYPE_BIGINT:
21292  if (db_get_bigint (rhs_val) > DB_INT32_MAX)
21293  lhs_less = true;
21294  else if (db_get_bigint (rhs_val) < DB_INT32_MIN)
21295  lhs_greater = true;
21296  break;
21297  case PT_TYPE_FLOAT:
21298  if (db_get_float (rhs_val) > DB_INT32_MAX)
21299  lhs_less = true;
21300  else if (db_get_float (rhs_val) < DB_INT32_MIN)
21301  lhs_greater = true;
21302  break;
21303 
21304  case PT_TYPE_DOUBLE:
21305  if (db_get_double (rhs_val) > DB_INT32_MAX)
21306  lhs_less = true;
21307  else if (db_get_double (rhs_val) < DB_INT32_MIN)
21308  lhs_greater = true;
21309  break;
21310 
21311  case PT_TYPE_NUMERIC:
21312  numeric_coerce_num_to_double (db_locate_numeric (rhs_val), DB_VALUE_SCALE (rhs_val), &dtmp);
21313  if (dtmp > DB_INT32_MAX)
21314  lhs_less = true;
21315  else if (dtmp < DB_INT32_MIN)
21316  lhs_greater = true;
21317  break;
21318 
21319  case PT_TYPE_MONETARY:
21320  dtmp = (db_get_monetary (rhs_val))->amount;
21321  if (dtmp > DB_INT32_MAX)
21322  lhs_less = true;
21323  else if (dtmp < DB_INT32_MIN)
21324  lhs_greater = true;
21325  break;
21326  default:
21327  break;
21328  }
21329  break;
21330 
21331  case PT_TYPE_BIGINT:
21332  switch (rhs_type)
21333  {
21334  case PT_TYPE_FLOAT:
21335  if (db_get_float (rhs_val) > DB_BIGINT_MAX)
21336  lhs_less = true;
21337  else if (db_get_float (rhs_val) < DB_BIGINT_MIN)
21338  lhs_greater = true;
21339  break;
21340  case PT_TYPE_DOUBLE:
21341  if (db_get_double (rhs_val) > DB_BIGINT_MAX)
21342  lhs_less = true;
21343  else if (db_get_double (rhs_val) < DB_BIGINT_MIN)
21344  lhs_greater = true;
21345  break;
21346  case PT_TYPE_NUMERIC:
21347  numeric_coerce_num_to_double (db_locate_numeric (rhs_val), DB_VALUE_SCALE (rhs_val), &dtmp);
21348  if (dtmp > DB_BIGINT_MAX)
21349  lhs_less = true;
21350  else if (dtmp < DB_BIGINT_MIN)
21351  lhs_greater = true;
21352  break;
21353  case PT_TYPE_MONETARY:
21354  dtmp = (db_get_monetary (rhs_val))->amount;
21355  if (dtmp > DB_BIGINT_MAX)
21356  lhs_less = true;
21357  else if (dtmp < DB_BIGINT_MIN)
21358  lhs_greater = true;
21359  break;
21360  default:
21361  break;
21362  }
21363  break;
21364 
21365  case PT_TYPE_FLOAT:
21366  switch (rhs_type)
21367  {
21368  case PT_TYPE_DOUBLE:
21369  if (db_get_double (rhs_val) > FLT_MAX)
21370  lhs_less = true;
21371  else if (db_get_double (rhs_val) < -(FLT_MAX))
21372  lhs_greater = true;
21373  break;
21374 
21375  case PT_TYPE_NUMERIC:
21376  numeric_coerce_num_to_double (db_locate_numeric (rhs_val), DB_VALUE_SCALE (rhs_val), &dtmp);
21377  if (dtmp > FLT_MAX)
21378  lhs_less = true;
21379  else if (dtmp < -(FLT_MAX))
21380  lhs_greater = true;
21381  break;
21382 
21383  case PT_TYPE_MONETARY:
21384  dtmp = (db_get_monetary (rhs_val))->amount;
21385  if (dtmp > FLT_MAX)
21386  lhs_less = true;
21387  else if (dtmp < -(FLT_MAX))
21388  lhs_greater = true;
21389  break;
21390 
21391  default:
21392  break;
21393  }
21394  break;
21395 
21396  default:
21397  break;
21398  }
21399 
21400  if (lhs_less)
21401  {
21402  if (op == PT_EQ || op == PT_GT || op == PT_GE)
21403  {
21404  always_false = true;
21405  }
21406  else if (op == PT_LT || op == PT_LE)
21407  {
21408  always_true = true;
21409  }
21410  }
21411  else if (lhs_greater)
21412  {
21413  if (op == PT_EQ || op == PT_LT || op == PT_LE)
21414  {
21415  always_false = true;
21416  }
21417  else if (op == PT_GT || op == PT_GE)
21418  {
21419  always_true = true;
21420  }
21421  }
21422 
21423 end:
21424  if (always_false)
21425  {
21426  result = parser_new_node (parser, PT_VALUE);
21427  if (result == NULL)
21428  {
21429  PT_INTERNAL_ERROR (parser, "allocate new node");
21430  return NULL;
21431  }
21432 
21433  result->type_enum = PT_TYPE_LOGICAL;
21434  result->info.value.data_value.i = false;
21435  result->info.value.location = expr->info.expr.location;
21436  (void) pt_value_to_db (parser, result);
21437  }
21438  else if (always_false_due_to_null)
21439  {
21440  result = parser_new_node (parser, PT_VALUE);
21441  if (result == NULL)
21442  {
21443  PT_INTERNAL_ERROR (parser, "allocate new node");
21444  return NULL;
21445  }
21446 
21447  result->type_enum = PT_TYPE_NULL;
21448  result->info.value.location = expr->info.expr.location;
21449  (void) pt_value_to_db (parser, result);
21450  }
21451  else if (always_true)
21452  {
21453  result = parser_new_node (parser, PT_VALUE);
21454  if (result == NULL)
21455  {
21456  PT_INTERNAL_ERROR (parser, "allocate new node");
21457  return NULL;
21458  }
21459 
21460  result->type_enum = PT_TYPE_LOGICAL;
21461  result->info.value.data_value.i = true;
21462  result->info.value.location = expr->info.expr.location;
21463  (void) pt_value_to_db (parser, result);
21464  }
21465 
21466  return result;
21467 }
21468 
21469 
21470 /*
21471  * pt_converse_op () - Figure out the converse of a relational operator,
21472  * so that we can flip a relational expression into a canonical form
21473  * return:
21474  * op(in):
21475  */
21476 
21477 PT_OP_TYPE
21479 {
21480  switch (op)
21481  {
21482  case PT_EQ:
21483  return PT_EQ;
21484  case PT_LT:
21485  return PT_GT;
21486  case PT_LE:
21487  return PT_GE;
21488  case PT_GT:
21489  return PT_LT;
21490  case PT_GE:
21491  return PT_LE;
21492  case PT_NE:
21493  return PT_NE;
21494  default:
21495  return (PT_OP_TYPE) 0;
21496  }
21497 }
21498 
21499 
21500 /*
21501  * pt_is_between_range_op () -
21502  * return:
21503  * op(in):
21504  */
21505 int
21507 {
21508  switch (op)
21509  {
21510  case PT_BETWEEN_AND:
21511  case PT_BETWEEN_GE_LE:
21512  case PT_BETWEEN_GE_LT:
21513  case PT_BETWEEN_GT_LE:
21514  case PT_BETWEEN_GT_LT:
21515  case PT_BETWEEN_EQ_NA:
21516  case PT_BETWEEN_INF_LE:
21517  case PT_BETWEEN_INF_LT:
21518  case PT_BETWEEN_GE_INF:
21519  case PT_BETWEEN_GT_INF:
21520  return 1;
21521  default:
21522  return 0;
21523  }
21524 }
21525 
21526 
21527 /*
21528  * pt_is_comp_op () -
21529  * return:
21530  * op(in):
21531  */
21532 int
21534 {
21535  switch (op)
21536  {
21537  case PT_LIKE:
21538  case PT_NOT_LIKE:
21539  case PT_IS_IN:
21540  case PT_IS_NOT_IN:
21541  case PT_IS_NULL:
21542  case PT_IS_NOT_NULL:
21543  case PT_IS:
21544  case PT_IS_NOT:
21545  case PT_EQ_SOME:
21546  case PT_NE_SOME:
21547  case PT_GE_SOME:
21548  case PT_GT_SOME:
21549  case PT_LT_SOME:
21550  case PT_LE_SOME:
21551  case PT_EQ_ALL:
21552  case PT_NE_ALL:
21553  case PT_GE_ALL:
21554  case PT_GT_ALL:
21555  case PT_LT_ALL:
21556  case PT_LE_ALL:
21557  case PT_EQ:
21558  case PT_NE:
21559  case PT_GE:
21560  case PT_GT:
21561  case PT_LT:
21562  case PT_LE:
21563  case PT_NULLSAFE_EQ:
21564  case PT_GT_INF:
21565  case PT_LT_INF:
21566  case PT_BETWEEN:
21567  case PT_BETWEEN_AND:
21568  case PT_BETWEEN_GE_LE:
21569  case PT_BETWEEN_GE_LT:
21570  case PT_BETWEEN_GT_LE:
21571  case PT_BETWEEN_GT_LT:
21572  case PT_BETWEEN_EQ_NA:
21573  case PT_BETWEEN_INF_LE:
21574  case PT_BETWEEN_INF_LT:
21575  case PT_BETWEEN_GE_INF:
21576  case PT_BETWEEN_GT_INF:
21577  case PT_RANGE:
21578  return 1;
21579  default:
21580  return 0;
21581  }
21582 }
21583 
21584 
21585 /*
21586  * pt_negate_op () -
21587  * return:
21588  * op(in):
21589  */
21590 PT_OP_TYPE
21592 {
21593  switch (op)
21594  {
21595  case PT_EQ:
21596  return PT_NE;
21597  case PT_NE:
21598  return PT_EQ;
21599  case PT_SETEQ:
21600  return PT_SETNEQ;
21601  case PT_SETNEQ:
21602  return PT_SETEQ;
21603  case PT_GT:
21604  return PT_LE;
21605  case PT_GE:
21606  return PT_LT;
21607  case PT_LT:
21608  return PT_GE;
21609  case PT_LE:
21610  return PT_GT;
21611  case PT_BETWEEN:
21612  return PT_NOT_BETWEEN;
21613  case PT_NOT_BETWEEN:
21614  return PT_BETWEEN;
21615  case PT_IS_IN:
21616  return PT_IS_NOT_IN;
21617  case PT_IS_NOT_IN:
21618  return PT_IS_IN;
21619  case PT_LIKE:
21620  return PT_NOT_LIKE;
21621  case PT_NOT_LIKE:
21622  return PT_LIKE;
21623  case PT_RLIKE:
21624  return PT_NOT_RLIKE;
21625  case PT_NOT_RLIKE:
21626  return PT_RLIKE;
21627  case PT_RLIKE_BINARY:
21628  return PT_NOT_RLIKE_BINARY;
21629  case PT_NOT_RLIKE_BINARY:
21630  return PT_RLIKE_BINARY;
21631  case PT_IS_NULL:
21632  return PT_IS_NOT_NULL;
21633  case PT_IS_NOT_NULL:
21634  return PT_IS_NULL;
21635  case PT_EQ_SOME:
21636  return PT_NE_ALL;
21637  case PT_NE_SOME:
21638  return PT_EQ_ALL;
21639  case PT_GT_SOME:
21640  return PT_LE_ALL;
21641  case PT_GE_SOME:
21642  return PT_LT_ALL;
21643  case PT_LT_SOME:
21644  return PT_GE_ALL;
21645  case PT_LE_SOME:
21646  return PT_GT_ALL;
21647  case PT_EQ_ALL:
21648  return PT_NE_SOME;
21649  case PT_NE_ALL:
21650  return PT_EQ_SOME;
21651  case PT_GT_ALL:
21652  return PT_LE_SOME;
21653  case PT_GE_ALL:
21654  return PT_LT_SOME;
21655  case PT_LT_ALL:
21656  return PT_GE_SOME;
21657  case PT_LE_ALL:
21658  return PT_GT_SOME;
21659  case PT_IS:
21660  return PT_IS_NOT;
21661  case PT_IS_NOT:
21662  return PT_IS;
21663  default:
21664  return (PT_OP_TYPE) 0;
21665  }
21666 }
21667 
21668 
21669 /*
21670  * pt_comp_to_between_op () -
21671  * return:
21672  * left(in):
21673  * right(in):
21674  * type(in):
21675  * between(out):
21676  */
21677 int
21679 {
21680  size_t i;
21681 
21682  for (i = 0; i < COMPARE_BETWEEN_OPERATOR_COUNT; i++)
21683  {
21684  if (left == pt_Compare_between_operator_table[i].left && right == pt_Compare_between_operator_table[i].right)
21685  {
21686  *between = pt_Compare_between_operator_table[i].between;
21687 
21688  return 0;
21689  }
21690  }
21691 
21692  if (type == PT_RANGE_INTERSECTION)
21693  { /* range intersection */
21694  if ((left == PT_GE && right == PT_EQ) || (left == PT_EQ && right == PT_LE))
21695  {
21696  *between = PT_BETWEEN_EQ_NA;
21697  return 0;
21698  }
21699  }
21700 
21701  return -1;
21702 }
21703 
21704 
21705 /*
21706  * pt_between_to_comp_op () -
21707  * return:
21708  * between(in):
21709  * left(out):
21710  * right(out):
21711  */
21712 int
21714 {
21715  size_t i;
21716 
21717  for (i = 0; i < COMPARE_BETWEEN_OPERATOR_COUNT; i++)
21718  if (between == pt_Compare_between_operator_table[i].between)
21719  {
21720  *left = pt_Compare_between_operator_table[i].left;
21721  *right = pt_Compare_between_operator_table[i].right;
21722 
21723  return 0;
21724  }
21725 
21726  return -1;
21727 }
21728 
21729 /*
21730  * pt_get_equivalent_type_with_op () - get the type to which a node should be
21731  * converted to in order to match an expression
21732  * definition;
21733  * return : the new type
21734  * def_type(in) : the type defined in the expression signature
21735  * arg_type(in) : the type of the received expression argument
21736  * op(in) : operator
21737  *
21738  * Note : this is a wrapper for 'pt_get_equivalent_type' : the default
21739  * equivalent type may be overridden for certain operators
21740  */
21741 static PT_TYPE_ENUM
21742 pt_get_equivalent_type_with_op (const PT_ARG_TYPE def_type, const PT_TYPE_ENUM arg_type, PT_OP_TYPE op)
21743 {
21744  if (pt_is_op_hv_late_bind (op) && (def_type.is_generic && arg_type == PT_TYPE_MAYBE))
21745  {
21746  /* leave undetermined type */
21747  return PT_TYPE_MAYBE;
21748  }
21749  return pt_get_equivalent_type (def_type, arg_type);
21750 }
21751 
21752 /*
21753  * pt_is_op_hv_late_bind () - checks if the operator is in the list of
21754  * operators that should perform late binding on
21755  * their host variable arguments
21756  *
21757  * return: true if arguments types should be mirrored
21758  * op(in): operator type
21759  *
21760  * Note: this functions is used by type inference algorithm to check if an
21761  * expression should leave its HV arguments as TYPE_MAYBE (the default
21762  * type inference behavior would be to match it with a concrete type
21763  * according to one of its signatures). Also, such expression is
21764  * wrapped with cast rather then its result type be forced to an
21765  * "expected domain" dictated by the expression context.
21766  */
21767 bool
21769 {
21770  switch (op)
21771  {
21772  case PT_ABS:
21773  case PT_CEIL:
21774  case PT_FLOOR:
21775  case PT_PLUS:
21776  case PT_DIVIDE:
21777  case PT_MODULUS:
21778  case PT_TIMES:
21779  case PT_MINUS:
21780  case PT_ROUND:
21781  case PT_TRUNC:
21782  case PT_UNARY_MINUS:
21783  case PT_EVALUATE_VARIABLE:
21784  case PT_DEFINE_VARIABLE:
21785  case PT_ADDTIME:
21786  case PT_TO_CHAR:
21787  case PT_HEX:
21788  case PT_CONV:
21789  case PT_ASCII:
21790  case PT_IFNULL:
21791  case PT_NVL:
21792  case PT_NVL2:
21793  case PT_COALESCE:
21794  case PT_NULLIF:
21795  case PT_LEAST:
21796  case PT_GREATEST:
21797  case PT_FROM_TZ:
21798  case PT_NEW_TIME:
21799  case PT_STR_TO_DATE:
21800  case PT_HOURF:
21801  case PT_MINUTEF:
21802  case PT_SECONDF:
21803  return true;
21804  default:
21805  return false;
21806  }
21807  return false;
21808 }
21809 
21810 /*
21811  * pt_wrap_expr_w_exp_dom_cast () - checks if the expression requires wrapping
21812  * with a cast to the type set in expected domain and performs the
21813  * wrap with cast if needed.
21814  *
21815  * return: new node (if wrap is performed), or unaltered node, if wrap is
21816  * not needed
21817  * parser(in): parser context
21818  * expr(in): expression node to be checked and wrapped
21819  */
21820 static PT_NODE *
21821 pt_wrap_expr_w_exp_dom_cast (PARSER_CONTEXT * parser, PT_NODE * expr)
21822 {
21823  /* expressions returning MAYBE, but with an expected domain are wrapped with cast */
21824  if (expr != NULL && expr->type_enum == PT_TYPE_MAYBE && pt_is_op_hv_late_bind (expr->info.expr.op)
21825  && expr->expected_domain != NULL)
21826  {
21827  PT_NODE *new_expr = NULL;
21828 
21829  if (expr->type_enum == PT_TYPE_ENUMERATION)
21830  {
21831  /* expressions should not return PT_TYPE_ENUMERATION */
21832  assert (false);
21833  PT_INTERNAL_ERROR (parser, "INVALID expected domain (PT_TYPE_ENUMERATION)");
21834  return NULL;
21835  }
21836 
21837  new_expr =
21840 
21841  if (new_expr != NULL)
21842  {
21843  /* reset expected domain of wrapped expression to NULL: it will be replaced at XASL generation with
21844  * DB_TYPE_VARIABLE domain */
21845  expr->expected_domain = NULL;
21846  expr = new_expr;
21847  }
21848  }
21849 
21850  return expr;
21851 }
21852 
21853 /*
21854  * pt_is_op_with_forced_common_type () - checks if the operator is in the list
21855  * of operators that should force its arguments to
21856  * the same type if none of the arguments has a
21857  * determined type
21858  *
21859  * return: true if arguments types should be mirrored
21860  * op(in): operator type
21861  *
21862  * Note: this functions is used by type inference algorithm
21863  */
21864 bool
21865 pt_is_op_with_forced_common_type (PT_OP_TYPE op)
21866 {
21867  switch (op)
21868  {
21869  case PT_IFNULL:
21870  case PT_COALESCE:
21871  case PT_NVL:
21872  case PT_NVL2:
21873  case PT_NULLIF:
21874  case PT_LEAST:
21875  case PT_GREATEST:
21876  case PT_BETWEEN:
21877  return true;
21878  default:
21879  return false;
21880  }
21881  return false;
21882 }
21883 
21884 /*
21885  * pt_check_const_fold_op_w_args () - checks if it is safe to perform constant
21886  * folding on the expression, given the
21887  * arguments' values
21888  * return: true if folding is allowed, false otherwise
21889  * op(in): a PT_OP_TYPE (the desired operation)
21890  * arg1(in): 1st db_value operand
21891  * arg2(in): 2nd db_value operand
21892  * arg3(in): 3rd db_value operand
21893  * domain(in): node domain
21894  *
21895  * Note : this function is used in context of constant folding to check if
21896  * folding an expression produces a large sized (string) result
21897  * which may cause performance problem
21898  */
21899 static bool
21900 pt_check_const_fold_op_w_args (PT_OP_TYPE op, DB_VALUE * arg1, DB_VALUE * arg2, DB_VALUE * arg3, TP_DOMAIN * domain)
21901 {
21902  const int MAX_RESULT_SIZE_ON_CONST_FOLDING = 256;
21903  switch (op)
21904  {
21905  case PT_CAST:
21906  if (TP_DOMAIN_TYPE (domain) == DB_TYPE_CLOB || TP_DOMAIN_TYPE (domain) == DB_TYPE_BLOB)
21907  {
21908  return false;
21909  }
21910  break;
21911 
21912  case PT_SPACE:
21913  if (DB_VALUE_DOMAIN_TYPE (arg1) == DB_TYPE_INTEGER)
21914  {
21915  int count_i = db_get_int (arg1);
21916  if (count_i > MAX_RESULT_SIZE_ON_CONST_FOLDING)
21917  {
21918  return false;
21919  }
21920  }
21921  else if (DB_VALUE_DOMAIN_TYPE (arg1) == DB_TYPE_SHORT)
21922  {
21923  short count_sh = db_get_short (arg1);
21924  if (count_sh > MAX_RESULT_SIZE_ON_CONST_FOLDING)
21925  {
21926  return false;
21927  }
21928  }
21929  else if (DB_VALUE_DOMAIN_TYPE (arg1) == DB_TYPE_BIGINT)
21930  {
21931  DB_BIGINT count_b = db_get_bigint (arg1);
21932  if (count_b > MAX_RESULT_SIZE_ON_CONST_FOLDING)
21933  {
21934  return false;
21935  }
21936  }
21937  break;
21938 
21939  case PT_REPEAT:
21940  if (DB_VALUE_DOMAIN_TYPE (arg2) == DB_TYPE_INTEGER)
21941  {
21942  int count_i = db_get_int (arg2);
21944  {
21945  int arg1_len = db_get_string_size (arg1);
21946 
21947  if (arg1_len * count_i > MAX_RESULT_SIZE_ON_CONST_FOLDING)
21948  {
21949  return false;
21950  }
21951  }
21952  }
21953  break;
21954 
21955  case PT_LPAD:
21956  case PT_RPAD:
21957  /* check if constant folding is OK */
21958  if (DB_VALUE_DOMAIN_TYPE (arg2) == DB_TYPE_INTEGER)
21959  {
21960  int count_i = db_get_int (arg2);
21961  if (arg3 != NULL && QSTR_IS_ANY_CHAR (DB_VALUE_DOMAIN_TYPE (arg3)))
21962  {
21963  int arg3_len = db_get_string_size (arg3);
21964 
21965  if (arg3_len * count_i > MAX_RESULT_SIZE_ON_CONST_FOLDING)
21966  {
21967  return false;
21968  }
21969  }
21970  }
21971  break;
21972 
21973  default:
21974  break;
21975  }
21976 
21977  return true;
21978 }
21979 
21980 /*
21981  * pt_is_op_w_collation () - check if is required to check collation or
21982  * codeset of this operator
21983  *
21984  * return:
21985  * node(in): a parse tree node
21986  *
21987  */
21988 static bool
21989 pt_is_op_w_collation (const PT_OP_TYPE op)
21990 {
21991  switch (op)
21992  {
21993  case PT_EQ:
21994  case PT_NE:
21995  case PT_GE:
21996  case PT_GT:
21997  case PT_LT:
21998  case PT_LE:
21999  case PT_NULLSAFE_EQ:
22000  case PT_BETWEEN:
22001  case PT_NOT_BETWEEN:
22002  case PT_BETWEEN_AND:
22003  case PT_BETWEEN_GE_LE:
22004  case PT_BETWEEN_GE_LT:
22005  case PT_BETWEEN_GT_LE:
22006  case PT_BETWEEN_GT_LT:
22007  case PT_CONCAT:
22008  case PT_CONCAT_WS:
22009  case PT_PLUS:
22010  case PT_LIKE:
22011  case PT_NOT_LIKE:
22012  case PT_SUBSTRING_INDEX:
22013  case PT_RPAD:
22014  case PT_LPAD:
22015  case PT_MID:
22016  case PT_SUBSTRING:
22017  case PT_REPLACE:
22018  case PT_TRANSLATE:
22019  case PT_COALESCE:
22020  case PT_STRCAT:
22021  case PT_TIME_FORMAT:
22022  case PT_DATE_FORMAT:
22023  case PT_TIMEF:
22024  case PT_DATEF:
22025  case PT_SETEQ:
22026  case PT_SETNEQ:
22027  case PT_SUBSET:
22028  case PT_SUBSETEQ:
22029  case PT_SUPERSET:
22030  case PT_SUPERSETEQ:
22031  case PT_GREATEST:
22032  case PT_LEAST:
22033  case PT_NULLIF:
22034  case PT_LOWER:
22035  case PT_UPPER:
22036  case PT_RTRIM:
22037  case PT_LTRIM:
22038  case PT_TRIM:
22039  case PT_LEFT:
22040  case PT_RIGHT:
22041  case PT_NVL:
22042  case PT_NVL2:
22043  case PT_IFNULL:
22044  case PT_IS_IN:
22045  case PT_IS_NOT_IN:
22046  case PT_EQ_SOME:
22047  case PT_NE_SOME:
22048  case PT_GE_SOME:
22049  case PT_GT_SOME:
22050  case PT_LT_SOME:
22051  case PT_LE_SOME:
22052  case PT_EQ_ALL:
22053  case PT_NE_ALL:
22054  case PT_GE_ALL:
22055  case PT_GT_ALL:
22056  case PT_LT_ALL:
22057  case PT_LE_ALL:
22058  case PT_FINDINSET:
22059  case PT_INSTR:
22060  case PT_LOCATE:
22061  case PT_POSITION:
22062  case PT_STRCMP:
22063  case PT_IF:
22064  case PT_FIELD:
22065  case PT_REVERSE:
22066  case PT_CONNECT_BY_ROOT:
22067  case PT_PRIOR:
22068  case PT_QPRIOR:
22069  case PT_INDEX_PREFIX:
22070  case PT_MINUS:
22071  return true;
22072  default:
22073  return false;
22074  }
22075  return false;
22076 }
22077 
22078 /*
22079  * pt_get_collation_info () - get the collation info of parse tree node
22080  *
22081  * return: true if node has collation
22082  * node(in): a parse tree node
22083  * coll_infer(out): collation inference data
22084  */
22085 bool
22087 {
22088  bool has_collation = false;
22089 
22090  assert (node != NULL);
22091  assert (coll_infer != NULL);
22092 
22093  coll_infer->coerc_level = PT_COLLATION_NOT_COERC;
22094  coll_infer->codeset = LANG_COERCIBLE_CODESET;
22095  coll_infer->coll_id = LANG_COERCIBLE_COLL;
22096  coll_infer->can_force_cs = false;
22097 
22098  if (node->data_type != NULL)
22099  {
22100  if (PT_HAS_COLLATION (node->type_enum))
22101  {
22102  coll_infer->coll_id = node->data_type->info.data_type.collation_id;
22103  coll_infer->codeset = (INTL_CODESET) node->data_type->info.data_type.units;
22104  has_collation = true;
22105 
22106  if (node->data_type->info.data_type.collation_flag == TP_DOMAIN_COLL_LEAVE)
22107  {
22108  coll_infer->can_force_cs = true;
22109  }
22110  }
22111  }
22112  else if (node->expected_domain != NULL)
22113  {
22114  if (TP_TYPE_HAS_COLLATION (TP_DOMAIN_TYPE (node->expected_domain)))
22115  {
22116  coll_infer->coll_id = TP_DOMAIN_COLLATION (node->expected_domain);
22117  coll_infer->codeset = TP_DOMAIN_CODESET (node->expected_domain);
22118  has_collation = true;
22119 
22120  if (TP_DOMAIN_COLLATION_FLAG (node->expected_domain) == TP_DOMAIN_COLL_LEAVE)
22121  {
22122  coll_infer->can_force_cs = true;
22123  }
22124  }
22125  }
22126  else if (node->type_enum == PT_TYPE_MAYBE || (node->node_type == PT_VALUE && PT_HAS_COLLATION (node->type_enum)))
22127  {
22128  coll_infer->coll_id = LANG_SYS_COLLATION;
22129  coll_infer->codeset = LANG_SYS_CODESET;
22130  has_collation = true;
22131 
22132  if (node->type_enum == PT_TYPE_MAYBE)
22133  {
22134  coll_infer->can_force_cs = true;
22135  }
22136  }
22137 
22138  if (has_collation && PT_GET_COLLATION_MODIFIER (node) != -1)
22139  {
22141 
22142  assert (lc != NULL);
22143 
22144  coll_infer->coll_id = PT_GET_COLLATION_MODIFIER (node);
22145 
22146  if (node->data_type != NULL)
22147  {
22148  assert (node->data_type->info.data_type.units == lc->codeset);
22149  }
22150  else if (node->expected_domain != NULL)
22151  {
22152  assert (TP_DOMAIN_CODESET (node->expected_domain) == lc->codeset);
22153  }
22154 
22155  coll_infer->coerc_level = PT_COLLATION_NOT_COERC;
22156  coll_infer->can_force_cs = false;
22157  return has_collation;
22158  }
22159 
22160  switch (node->node_type)
22161  {
22162  case PT_VALUE:
22163  if (coll_infer->coll_id == LANG_COLL_BINARY)
22164  {
22166  }
22167  else if (LANG_IS_COERCIBLE_COLL (coll_infer->coll_id))
22168  {
22169  coll_infer->coerc_level = PT_COLLATION_L4_BIN_COERC;
22170  }
22171  else
22172  {
22173  coll_infer->coerc_level = PT_COLLATION_L4_COERC;
22174  }
22175  break;
22176 
22177  case PT_HOST_VAR:
22178  coll_infer->coerc_level = PT_COLLATION_FULLY_COERC;
22179  coll_infer->can_force_cs = true;
22180  break;
22181 
22182  case PT_EXPR:
22183  if (node->info.expr.op == PT_CURRENT_USER || node->info.expr.op == PT_USER || node->info.expr.op == PT_DATABASE
22184  || node->info.expr.op == PT_SCHEMA || node->info.expr.op == PT_VERSION)
22185  {
22186  coll_infer->coerc_level = PT_COLLATION_L3_COERC;
22187  break;
22188  }
22189 
22190  if (node->info.expr.op == PT_EVALUATE_VARIABLE || node->info.expr.op == PT_DEFINE_VARIABLE)
22191  {
22192  coll_infer->coerc_level = PT_COLLATION_FULLY_COERC;
22193  break;
22194  }
22195 
22197  {
22198  PT_COLL_INFER coll_infer_dummy;
22199  /* collation and codeset of wrapped CAST, but get coercibility from original node */
22200  pt_get_collation_info (node->info.expr.arg1, &coll_infer_dummy);
22201  coll_infer->coerc_level = coll_infer_dummy.coerc_level;
22202 
22203  if (!PT_HAS_COLLATION (node->info.expr.arg1->type_enum) && node->info.expr.arg1->type_enum != PT_TYPE_MAYBE)
22204  {
22205  coll_infer->can_force_cs = true;
22206  }
22207  break;
22208  }
22209  /* fall through */
22210  case PT_SELECT:
22211  case PT_UNION:
22212  case PT_DIFFERENCE:
22213  case PT_INTERSECTION:
22214  case PT_FUNCTION:
22215  case PT_METHOD_CALL:
22216  if (coll_infer->coll_id == LANG_COLL_BINARY)
22217  {
22219  }
22220  else if (LANG_IS_COERCIBLE_COLL (coll_infer->coll_id))
22221  {
22222  coll_infer->coerc_level = PT_COLLATION_L2_BIN_COERC;
22223  }
22224  else
22225  {
22226  coll_infer->coerc_level = PT_COLLATION_L2_COERC;
22227  }
22228  break;
22229 
22230  case PT_NAME:
22232  {
22233  if (coll_infer->coll_id == LANG_COLL_BINARY)
22234  {
22236  }
22237  else if (LANG_IS_COERCIBLE_COLL (coll_infer->coll_id))
22238  {
22239  coll_infer->coerc_level = PT_COLLATION_L2_BIN_COERC;
22240  }
22241  else
22242  {
22243  coll_infer->coerc_level = PT_COLLATION_L2_COERC;
22244  }
22245  break;
22246  }
22247  else if (pt_is_input_parameter (node) || node->type_enum == PT_TYPE_MAYBE)
22248  {
22249  coll_infer->coerc_level = PT_COLLATION_L5_COERC;
22250  break;
22251  }
22252  /* Fall through */
22253  case PT_DOT_:
22254  if (coll_infer->coll_id == LANG_COLL_BINARY)
22255  {
22257  }
22258  else if (LANG_IS_COERCIBLE_COLL (coll_infer->coll_id))
22259  {
22260  coll_infer->coerc_level = PT_COLLATION_L1_BIN_COERC;
22261  }
22262  else
22263  {
22264  coll_infer->coerc_level = PT_COLLATION_L1_COERC;
22265  }
22266  break;
22267 
22268  default:
22269  coll_infer->coerc_level = PT_COLLATION_NOT_COERC;
22270  }
22271 
22272  return has_collation;
22273 }
22274 
22275 /*
22276  * pt_get_collation_info_for_collection_type () - get the collation info of a
22277  * parse tree node with collection type
22278  *
22279  * return: NO_COLLATION = node doesn't have collation;
22280  * HAS_COLLATION = node has collation
22281  * ERROR_COLLATION = node has multiple component types with
22282  * collation and collations are not compatible
22283  *
22284  * parser(in)
22285  * node(in): a parse tree node
22286  * coll_infer(out): collation inference data
22287  *
22288  */
22289 static COLLATION_RESULT
22290 pt_get_collation_info_for_collection_type (PARSER_CONTEXT * parser, const PT_NODE * node, PT_COLL_INFER * coll_infer)
22291 {
22292  bool has_collation = false;
22293  bool is_collection_of_collection = false;
22294  bool first_element = true;
22295  int status_inner_collection;
22296 
22297  assert (node != NULL);
22298  assert (coll_infer != NULL);
22299 
22300  coll_infer->coerc_level = PT_COLLATION_NOT_COERC;
22301  coll_infer->codeset = LANG_COERCIBLE_CODESET;
22302  coll_infer->coll_id = LANG_COERCIBLE_COLL;
22303  coll_infer->can_force_cs = false;
22304 
22305  if (node->node_type == PT_HOST_VAR && node->expected_domain != NULL
22306  && TP_IS_SET_TYPE (TP_DOMAIN_TYPE (node->expected_domain)))
22307  {
22308  coll_infer->coerc_level = PT_COLLATION_FULLY_COERC;
22309  coll_infer->can_force_cs = true;
22310  return HAS_COLLATION;
22311  }
22312 
22313  assert (PT_IS_COLLECTION_TYPE (node->type_enum));
22314 
22315  if (node->data_type != NULL)
22316  {
22317  const PT_NODE *current_set_node = node;
22318 
22319  /* if node is a collection of collection, advance to the first element of it */
22320  if ((node->node_type == PT_FUNCTION) && (node->info.function.arg_list != NULL))
22321  {
22322  if (((node->info.function.function_type == F_TABLE_SET)
22323  || (node->info.function.function_type == F_TABLE_MULTISET)
22324  || (node->info.function.function_type == F_TABLE_SEQUENCE))
22325  && (node->info.function.arg_list->node_type == PT_SELECT))
22326  {
22327  current_set_node = node->info.function.arg_list->info.query.q.select.list;
22328  is_collection_of_collection = true;
22329  }
22330  else if ((node->info.function.function_type == F_SET) || (node->info.function.function_type == F_MULTISET)
22331  || (node->info.function.function_type == F_SEQUENCE))
22332  {
22333  current_set_node = node->info.function.arg_list;
22334  is_collection_of_collection = true;
22335  }
22336  }
22337  else if ((node->node_type == PT_VALUE) && (PT_IS_COLLECTION_TYPE (node->type_enum)))
22338  {
22339  current_set_node = node->info.value.data_value.set;
22340  is_collection_of_collection = true;
22341  }
22342 
22343  if (is_collection_of_collection)
22344  {
22345  /* go through the elements of the collection and check their collations */
22346  while (current_set_node != NULL)
22347  {
22348  status_inner_collection =
22349  pt_get_collation_of_collection (parser, current_set_node, coll_infer, true, &first_element);
22350 
22351  if (status_inner_collection == HAS_COLLATION)
22352  {
22353  has_collation = true;
22354  }
22355  else if (status_inner_collection == ERROR_COLLATION)
22356  {
22357  goto error;
22358  }
22359  current_set_node = current_set_node->next;
22360  }
22361  }
22362  else
22363  {
22364  status_inner_collection =
22365  pt_get_collation_of_collection (parser, current_set_node->data_type, coll_infer, false, &first_element);
22366 
22367  if (status_inner_collection == HAS_COLLATION)
22368  {
22369  has_collation = true;
22370  }
22371  else if (status_inner_collection == ERROR_COLLATION)
22372  {
22373  goto error;
22374  }
22375  }
22376  }
22377  else
22378  {
22379  if (node->node_type == PT_FUNCTION && node->info.function.arg_list != NULL
22380  && node->info.function.arg_list->type_enum == PT_TYPE_MAYBE)
22381  {
22382  /* charset and collation of system */
22383  has_collation = true;
22384 
22385  if (node->info.function.function_type == F_SET || node->info.function.function_type == F_MULTISET
22386  || node->info.function.function_type == F_SEQUENCE)
22387  {
22388  coll_infer->can_force_cs = true;
22389  coll_infer->coerc_level = PT_COLLATION_L5_COERC;
22390  coll_infer->codeset = LANG_COERCIBLE_CODESET;
22391  coll_infer->coll_id = LANG_COERCIBLE_COLL;
22392  return HAS_COLLATION;
22393  }
22394  }
22395  }
22396 
22397  if (!has_collation)
22398  {
22399  return NO_COLLATION;
22400  }
22401 
22402  if (has_collation && PT_GET_COLLATION_MODIFIER (node) != -1)
22403  {
22405 
22406  assert (lc != NULL);
22407 
22408  coll_infer->coll_id = PT_GET_COLLATION_MODIFIER (node);
22409 
22410  assert (coll_infer->codeset == lc->codeset);
22411 
22412  coll_infer->coerc_level = PT_COLLATION_NOT_COERC;
22413  return HAS_COLLATION;
22414  }
22415 
22416  switch (node->node_type)
22417  {
22418  case PT_VALUE:
22419  assert (has_collation);
22420  if (coll_infer->coll_id == LANG_COLL_BINARY)
22421  {
22423  }
22424  else if (LANG_IS_COERCIBLE_COLL (coll_infer->coll_id))
22425  {
22426  coll_infer->coerc_level = PT_COLLATION_L4_BIN_COERC;
22427  }
22428  else
22429  {
22430  coll_infer->coerc_level = PT_COLLATION_L4_COERC;
22431  }
22432  break;
22433 
22434  case PT_HOST_VAR:
22435  assert (has_collation);
22436  coll_infer->coerc_level = PT_COLLATION_FULLY_COERC;
22437  break;
22438 
22439  case PT_EXPR:
22440  case PT_FUNCTION:
22441  case PT_SELECT:
22442  case PT_UNION:
22443  case PT_DIFFERENCE:
22444  case PT_INTERSECTION:
22445  if ((!has_collation && LANG_SYS_COLLATION == LANG_COLL_BINARY) || (coll_infer->coll_id == LANG_COLL_BINARY))
22446  {
22448  }
22449  else if (!has_collation || LANG_IS_COERCIBLE_COLL (coll_infer->coll_id))
22450  {
22451  coll_infer->coerc_level = PT_COLLATION_L2_BIN_COERC;
22452  }
22453  else
22454  {
22455  coll_infer->coerc_level = PT_COLLATION_L2_COERC;
22456  }
22457  break;
22458 
22459  case PT_NAME:
22460  case PT_DOT_:
22461  assert (has_collation);
22462  if (coll_infer->coll_id == LANG_COLL_BINARY)
22463  {
22465  }
22466  else if (LANG_IS_COERCIBLE_COLL (coll_infer->coll_id))
22467  {
22468  coll_infer->coerc_level = PT_COLLATION_L1_BIN_COERC;
22469  }
22470  else
22471  {
22472  coll_infer->coerc_level = PT_COLLATION_L1_COERC;
22473  }
22474  break;
22475 
22476  default:
22477  assert (!has_collation);
22478  coll_infer->coerc_level = PT_COLLATION_NOT_COERC;
22479  }
22480 
22481  return has_collation ? HAS_COLLATION : NO_COLLATION;
22482 
22483 error:
22485 
22486  return ERROR_COLLATION;
22487 }
22488 
22489 /*
22490  * pt_get_collation_of_collection () - get the collation info of a
22491  * parse tree node with a collection type
22492  *
22493  * return: NO_COLLATION = node doesn't have collation;
22494  * HAS_COLLATION = node has collation
22495  * ERROR_COLLATION = node has multiple component types with
22496  * collation and collations are not compatible
22497  *
22498  * parser(in)
22499  * node(in): a parse tree node
22500  * coll_infer(out): collation inference data
22501  * is_inner_collection(in): the node is an inner collection (inside
22502  * another collection)
22503  * first_element(in/out): is this the first element of the outer collection
22504  * (of all of the collections of collection)
22505  *
22506  */
22507 static COLLATION_RESULT
22508 pt_get_collation_of_collection (PARSER_CONTEXT * parser, const PT_NODE * node, PT_COLL_INFER * coll_infer,
22509  const bool is_inner_collection, bool * is_first_element)
22510 {
22511  const PT_NODE *current_node;
22512  bool has_collation = false;
22513 
22514  /* check all collatable types of collection */
22515  while (node != NULL)
22516  {
22517  if (is_inner_collection)
22518  {
22519  current_node = node->data_type;
22520  }
22521  else
22522  {
22523  current_node = node;
22524  }
22525 
22526  if (current_node != NULL)
22527  {
22528  if (PT_IS_COLLECTION_TYPE (node->type_enum))
22529  {
22530  PT_COLL_INFER coll_infer_elem;
22531  int status;
22532 
22533  status = pt_get_collation_info_for_collection_type (parser, node, &coll_infer_elem);
22534 
22535  if (status == HAS_COLLATION)
22536  {
22537  has_collation = true;
22538  }
22539  else if (status == ERROR_COLLATION)
22540  {
22541  goto error;
22542  }
22543 
22544  if (*is_first_element)
22545  {
22546  coll_infer->coll_id = coll_infer_elem.coll_id;
22547  coll_infer->codeset = coll_infer_elem.codeset;
22548  *is_first_element = false;
22549  }
22550  else if ((coll_infer_elem.coll_id != coll_infer->coll_id)
22551  || (coll_infer_elem.codeset != coll_infer->codeset))
22552  {
22553  /* error : different collations in same collection */
22554  goto error;
22555  }
22556  }
22557  else if (PT_HAS_COLLATION (current_node->type_enum))
22558  {
22559  assert (current_node->node_type == PT_DATA_TYPE);
22560 
22561  has_collation = true;
22562  if (*is_first_element)
22563  {
22564  coll_infer->codeset = (INTL_CODESET) current_node->info.data_type.units;
22565  coll_infer->coll_id = current_node->info.data_type.collation_id;
22566  *is_first_element = false;
22567  }
22568  else if ((coll_infer->coll_id != current_node->info.data_type.collation_id)
22569  || (coll_infer->codeset != current_node->info.data_type.units))
22570  {
22571  /* error : different collations in same collection */
22572  goto error;
22573  }
22574  }
22575  }
22576  node = node->next;
22577  }
22578 
22579  return has_collation ? HAS_COLLATION : NO_COLLATION;
22580 
22581 error:
22583 
22584  return ERROR_COLLATION;
22585 }
22586 
22587 /*
22588  * pt_coerce_node_collection_of_collection () - changes the collation of a
22589  * collection type parse tree node
22590  *
22591  * return: parse node after coercion
22592  * parser(in)
22593  * node(in): a parse tree node
22594  * coll_id(in): collation
22595  * codeset(in): codeset
22596  * force_mode(in): true if codeset and collation have to forced
22597  * use_collate_modifier(in): true if collation coercion should be done using
22598  * a COLLATE expression modifier
22599  * wrap_type_for_maybe(in): type to use for wrap with cast when current type
22600  * is uncertain (PT_TYPE_MAYBE)
22601  * wrap_type_collection(in): collection type to use for wrap with cast when
22602  * current type is uncertain; if this value is not
22603  * of collection type, then the wrap is without
22604  * a collection
22605  */
22606 static PT_NODE *
22607 pt_coerce_node_collection_of_collection (PARSER_CONTEXT * parser, PT_NODE * node, const int coll_id,
22608  const INTL_CODESET codeset, bool force_mode, bool use_collate_modifier,
22609  PT_TYPE_ENUM wrap_type_for_maybe, PT_TYPE_ENUM wrap_type_collection)
22610 {
22611  PT_NODE *current_set_node = NULL, *prev_node = NULL, *save_next = NULL;
22612  bool is_collection_of_collection = false;
22613 
22614  if (node->data_type != NULL)
22615  {
22616  /* if node is a collection of collection, advance to the first element of it */
22617  if ((node->node_type == PT_FUNCTION) && (node->info.function.arg_list != NULL))
22618  {
22619  if (((node->info.function.function_type == F_TABLE_SET)
22620  || (node->info.function.function_type == F_TABLE_MULTISET)
22621  || (node->info.function.function_type == F_TABLE_SEQUENCE))
22622  && (node->info.function.arg_list->node_type == PT_SELECT))
22623  {
22624  current_set_node = node->info.function.arg_list->info.query.q.select.list;
22625  is_collection_of_collection = true;
22626  }
22627  else if ((node->info.function.function_type == F_SET) || (node->info.function.function_type == F_MULTISET)
22628  || (node->info.function.function_type == F_SEQUENCE))
22629  {
22630  current_set_node = node->info.function.arg_list;
22631  is_collection_of_collection = true;
22632  }
22633  }
22634  else if ((node->node_type == PT_VALUE) && (PT_IS_COLLECTION_TYPE (node->type_enum)))
22635  {
22636  current_set_node = node->info.value.data_value.set;
22637  is_collection_of_collection = true;
22638  }
22639 
22640  if (is_collection_of_collection == true)
22641  {
22642  assert (current_set_node != NULL);
22643  /* change the elements of the collection by applying the new collation to them */
22644  while (current_set_node != NULL)
22645  {
22646  save_next = current_set_node->next;
22647 
22648  current_set_node =
22649  pt_coerce_node_collation (parser, current_set_node, coll_id, codeset, force_mode, use_collate_modifier,
22650  wrap_type_for_maybe, wrap_type_collection);
22651 
22652  if (current_set_node != NULL)
22653  {
22654  if (prev_node == NULL)
22655  {
22656  if ((node->node_type == PT_FUNCTION) && (node->info.function.arg_list != NULL))
22657  {
22658  if (((node->info.function.function_type == F_TABLE_SET)
22659  || (node->info.function.function_type == F_TABLE_MULTISET)
22660  || (node->info.function.function_type == F_TABLE_SEQUENCE))
22661  && (node->info.function.arg_list->node_type == PT_SELECT))
22662  {
22663  node->info.function.arg_list->info.query.q.select.list = current_set_node;
22664  }
22665  else if ((node->info.function.function_type == F_SET)
22666  || (node->info.function.function_type == F_MULTISET)
22667  || (node->info.function.function_type == F_SEQUENCE))
22668  {
22669  node->info.function.arg_list = current_set_node;
22670  }
22671  }
22672  else if ((node->node_type == PT_VALUE) && (PT_IS_COLLECTION_TYPE (node->type_enum)))
22673  {
22674  node->info.value.data_value.set = current_set_node;
22675  }
22676  }
22677  else
22678  {
22679  assert (prev_node != NULL);
22680  prev_node->next = current_set_node;
22681  }
22682 
22683  current_set_node->next = save_next;
22684  }
22685  else
22686  {
22687  assert (current_set_node == NULL);
22688  goto cannot_coerce;
22689  }
22690  prev_node = current_set_node;
22691  current_set_node = current_set_node->next;
22692  } /* while (current_set_node != NULL) */
22693 
22694  if (node->node_type == PT_VALUE)
22695  {
22696  node->info.value.db_value_is_initialized = 0;
22697  (void) pt_value_to_db (parser, node);
22698  }
22699  } /* if (is_collection_of_collection == true) */
22700  }
22701 
22702  return node;
22703 
22704 cannot_coerce:
22705  if (codeset != LANG_COERCIBLE_CODESET || !LANG_IS_COERCIBLE_COLL (coll_id))
22706  {
22707  return NULL;
22708  }
22709  return node;
22710 }
22711 
22712 /*
22713  * pt_coerce_node_collation () - changes the collation of parse tree node
22714  *
22715  * return: parse node after coercion
22716  * node(in): a parse tree node
22717  * coll_id(in): collation
22718  * codeset(in): codeset
22719  * force_mode(in): true if codeset and collation have to forced
22720  * use_collate_modifier(in): true if collation coercion should be done using
22721  * a COLLATE expression modifier
22722  * wrap_type_for_maybe(in): type to use for wrap with cast when current type
22723  * is uncertain (PT_TYPE_MAYBE)
22724  * wrap_type_collection(in): collection type to use for wrap with cast when
22725  * current type is uncertain; if this value is not
22726  * of collection type, then the wrap is without
22727  * a collection
22728  *
22729  * Note : 'force_mode' controlls how new collation and charset are applied:
22730  * When 'force_mode' in set, collation and charset are forced;
22731  * if not set, and codeset change is detected, then collation
22732  * coercion will require a CAST (wrap with cast) or a value coerce
22733  * in order to ensure charset conversion. If charset doesn't change,
22734  * it is safe to force directly the new collation.
22735  * When 'node' is an argument of an expression 'force_mode' is false;
22736  * 'force_mode' is on when 'node' is a result (expression) : for this
22737  * case it is assumed the algorithm ensures the result's collation and
22738  * codeset by previously applying the coercion on its arguments.
22739  *
22740  * use_collate_modifier : if true and wrap_with_cast is done, the
22741  * CAST operator is flagged with PT_EXPR_INFO_CAST_COLL_MODIFIER; this
22742  * flag is set only when CAST operation does not change charset, but
22743  * only collation; this kind of CAST is not transformed into T_CAST
22744  * during XASL generation, but into a flagged REGU_VARIABLE which
22745  * "knows" to "update" its collation after "fetch". See usage of
22746  * REGU_VARIABLE_APPLY_COLLATION flag.
22747  *
22748  */
22749 static PT_NODE *
22750 pt_coerce_node_collation (PARSER_CONTEXT * parser, PT_NODE * node, const int coll_id, const INTL_CODESET codeset,
22751  bool force_mode, bool use_collate_modifier, PT_TYPE_ENUM wrap_type_for_maybe,
22752  PT_TYPE_ENUM wrap_type_collection)
22753 {
22754  PT_NODE_TYPE original_node_type;
22755  PT_NODE *wrap_dt;
22756  PT_NODE *collection_node;
22757  bool preset_hv_in_collection = false;
22758  bool is_string_literal = false;
22759 
22760  assert (node != NULL);
22761 
22762  wrap_dt = NULL;
22763 
22764  collection_node =
22765  pt_coerce_node_collection_of_collection (parser, node, coll_id, codeset, force_mode, use_collate_modifier,
22766  wrap_type_for_maybe, wrap_type_collection);
22767  if (collection_node != node)
22768  {
22769  return collection_node;
22770  }
22771 
22772  original_node_type = node->node_type;
22773  switch (node->node_type)
22774  {
22775  case PT_NAME:
22776  case PT_DOT_:
22777  if (node->type_enum == PT_TYPE_MAYBE)
22778  {
22779  /* wrap with cast */
22780  wrap_dt = parser_new_node (parser, PT_DATA_TYPE);
22781  if (wrap_dt == NULL)
22782  {
22783  goto cannot_coerce;
22784  }
22785 
22786  assert (PT_IS_CHAR_STRING_TYPE (wrap_type_for_maybe));
22787 
22788  wrap_dt->type_enum = wrap_type_for_maybe;
22790  wrap_dt->info.data_type.collation_id = coll_id;
22791  wrap_dt->info.data_type.units = codeset;
22793  force_mode = false;
22794  }
22795  else if (!PT_HAS_COLLATION (node->type_enum) && !PT_IS_COLLECTION_TYPE (node->type_enum))
22796  {
22797  goto cannot_coerce;
22798  }
22799  break;
22800  case PT_EXPR:
22801  case PT_SELECT:
22802  case PT_FUNCTION:
22803  case PT_METHOD_CALL:
22804  case PT_UNION:
22805  case PT_INTERSECTION:
22806  case PT_DIFFERENCE:
22807  if (!PT_HAS_COLLATION (node->type_enum) && !PT_IS_COLLECTION_TYPE (node->type_enum)
22808  && node->type_enum != PT_TYPE_MAYBE)
22809  {
22810  goto cannot_coerce;
22811  }
22812 
22813  if ((node->data_type == NULL && node->type_enum == PT_TYPE_MAYBE)
22814  || (PT_IS_COLLECTION_TYPE (node->type_enum) && node->node_type == PT_FUNCTION))
22815  {
22816  /* wrap with cast */
22817  wrap_dt = parser_new_node (parser, PT_DATA_TYPE);
22818  if (wrap_dt == NULL)
22819  {
22820  goto cannot_coerce;
22821  }
22822 
22823  assert (PT_IS_CHAR_STRING_TYPE (wrap_type_for_maybe));
22824 
22825  wrap_dt->type_enum = wrap_type_for_maybe;
22827  wrap_dt->info.data_type.collation_id = coll_id;
22828  wrap_dt->info.data_type.units = codeset;
22830  force_mode = false;
22831  }
22832  break;
22833  case PT_VALUE:
22834  if (node->data_type == NULL && PT_HAS_COLLATION (node->type_enum) && coll_id != LANG_SYS_COLLATION)
22835  {
22836  /* create a data type */
22837  node->data_type = parser_new_node (parser, PT_DATA_TYPE);
22838  if (node->data_type == NULL)
22839  {
22840  goto cannot_coerce;
22841  }
22842 
22843  node->data_type->type_enum = node->type_enum;
22844  node->data_type->info.data_type.precision = TP_FLOATING_PRECISION_VALUE;
22845  node->data_type->info.data_type.collation_id = LANG_SYS_COLLATION;
22846  node->data_type->info.data_type.units = LANG_SYS_CODESET;
22847  }
22848  break;
22849  case PT_HOST_VAR:
22850  if (node->type_enum == PT_TYPE_MAYBE && node->expected_domain == NULL)
22851  {
22852  TP_DOMAIN *dom_hv;
22853  DB_TYPE exp_db_type;
22854 
22855  assert (PT_IS_CHAR_STRING_TYPE (wrap_type_for_maybe));
22856 
22857  if (PT_IS_COLLECTION_TYPE (wrap_type_collection))
22858  {
22859  exp_db_type = pt_type_enum_to_db (wrap_type_collection);
22860  dom_hv = tp_domain_resolve_default (exp_db_type);
22861  }
22862  else
22863  {
22864  exp_db_type = pt_type_enum_to_db (wrap_type_for_maybe);
22865  dom_hv = tp_domain_resolve_default_w_coll (exp_db_type, coll_id, TP_DOMAIN_COLL_ENFORCE);
22866  }
22867  dom_hv = tp_domain_cache (dom_hv);
22868  SET_EXPECTED_DOMAIN (node, dom_hv);
22869  }
22870  break;
22871  default:
22872  /* by default, no not coerce */
22873  goto cannot_coerce;
22874  }
22875 
22876  if (node->node_type == PT_VALUE && PT_IS_CHAR_STRING_TYPE (node->type_enum)
22877  && node->info.value.is_collate_allowed == false)
22878  {
22879  is_string_literal = true;
22880  }
22881 
22882  if (node->data_type != NULL || wrap_dt != NULL)
22883  {
22884  assert (PT_IS_COLLECTION_TYPE (node->type_enum) || PT_HAS_COLLATION (node->type_enum)
22885  || node->type_enum == PT_TYPE_MAYBE);
22886 
22887  if (PT_IS_COLLECTION_TYPE (node->type_enum))
22888  {
22889  PT_NODE *dt_node;
22890  PT_NODE *dt = NULL, *arg;
22891  bool apply_wrap_cast = false;
22892 
22893  if (node->data_type == NULL)
22894  {
22895  assert (wrap_dt != NULL);
22896  /* collection without data type : any (?, ?) */
22897  node->data_type = wrap_dt;
22898  force_mode = true;
22899  preset_hv_in_collection = true;
22900  }
22901 
22902  assert (node->data_type != NULL);
22903  dt_node = node->data_type;
22904 
22905  /* check if wrap with cast is necessary */
22906  if (!force_mode || node->node_type != PT_EXPR || node->info.expr.op != PT_CAST)
22907  {
22908  do
22909  {
22910  if (PT_HAS_COLLATION (dt_node->type_enum) && dt_node->info.data_type.collation_id != coll_id)
22911  {
22912  apply_wrap_cast = true;
22913  break;
22914  }
22915 
22916  dt_node = dt_node->next;
22917  }
22918  while (dt_node != NULL);
22919 
22920  if (apply_wrap_cast == false && node->node_type == PT_FUNCTION
22921  && ((node->info.function.function_type == F_MULTISET) || (node->info.function.function_type == F_SET)
22922  || (node->info.function.function_type == F_SEQUENCE)))
22923  {
22924  arg = node->info.function.arg_list;
22925  do
22926  {
22927  if ((PT_HAS_COLLATION (arg->type_enum) && arg->data_type != NULL
22928  && (arg->data_type->info.data_type.collation_id != coll_id))
22929  || (arg->type_enum == PT_TYPE_MAYBE && arg->node_type != PT_HOST_VAR))
22930  {
22931  apply_wrap_cast = true;
22932  break;
22933  }
22934 
22935  arg = arg->next;
22936  }
22937  while (arg != NULL);
22938  }
22939  }
22940 
22941  if (apply_wrap_cast)
22942  {
22943  dt = parser_copy_tree_list (parser, node->data_type);
22944  dt_node = dt;
22945  }
22946  else
22947  {
22948  dt_node = node->data_type;
22949  }
22950 
22951  /* apply new collation and codeset for all collatable sub-types */
22952  do
22953  {
22954  if (PT_HAS_COLLATION (dt_node->type_enum))
22955  {
22956  dt_node->info.data_type.collation_id = coll_id;
22957  dt_node->info.data_type.units = (int) codeset;
22958  if (!PT_IS_COLLECTION_TYPE (node->type_enum))
22959  {
22961  }
22962  else
22963  {
22965  }
22966  }
22967 
22968  dt_node = dt_node->next;
22969  }
22970  while (dt_node != NULL);
22971 
22972  if (apply_wrap_cast)
22973  {
22974  node = pt_wrap_collection_with_cast_op (parser, node, node->type_enum, dt, true);
22975  /* flag PT_EXPR_INFO_CAST_COLL_MODIFIER is not set here; COLLATE modifier is not supported for this
22976  * context */
22977  }
22978  else if (dt != NULL)
22979  {
22980  parser_free_node (parser, dt);
22981  }
22982  }
22983  else if (PT_HAS_COLLATION (node->type_enum) || node->type_enum == PT_TYPE_MAYBE)
22984  {
22985  /* We wrap with cast when: - force_mode is disabled (we apply new collation on existing node), and - it is a
22986  * string literal node with different codeset - it is a other node type with differrent collation - it is not
22987  * a CAST expression - it is not HOST_VAR node */
22988  if (!force_mode
22989  && ((node->data_type != NULL
22990  && ((is_string_literal == false && node->data_type->info.data_type.collation_id != coll_id)
22991  || node->data_type->info.data_type.units != codeset)) || wrap_dt != NULL)
22992  && (node->node_type != PT_EXPR || node->info.expr.op != PT_CAST
22993  || pt_cast_needs_wrap_for_collation (node, codeset)) && node->node_type != PT_HOST_VAR)
22994  {
22995  if (wrap_dt == NULL)
22996  {
22997  wrap_dt = parser_copy_tree_list (parser, node->data_type);
22998  wrap_dt->info.data_type.collation_id = coll_id;
22999  wrap_dt->info.data_type.units = (int) codeset;
23001  }
23002  if (node->node_type == PT_VALUE && codeset == INTL_CODESET_RAW_BYTES
23003  && node->data_type->info.data_type.units != INTL_CODESET_RAW_BYTES)
23004  {
23005  /* cannot have values of ENUM type here */
23006  assert (PT_IS_CHAR_STRING_TYPE (node->type_enum));
23007  /* converting from multibyte charset to binary charset, may truncate the string data (precision is
23008  * kept); this workaround ensures that new precision (after charset conversion) grows to the size in
23009  * bytes of original data: conversion rule from multibyte charset to binary is to reinterpret the
23010  * bytes as binary characters. */
23011  if (node->info.value.data_value.str != NULL)
23012  {
23013  wrap_dt->info.data_type.precision = node->info.value.data_value.str->length;
23014  }
23015  else if (node->info.value.db_value_is_initialized)
23016  {
23017  wrap_dt->info.data_type.precision = db_get_string_size (&(node->info.value.db_value));
23018  }
23019  }
23020 
23021  if (node->node_type == PT_SELECT || node->node_type == PT_DIFFERENCE || node->node_type == PT_INTERSECTION
23022  || node->node_type == PT_UNION)
23023  {
23024  PT_NODE *select_list;
23025  int nb_select_list;
23026 
23027  if (node->node_type == PT_SELECT)
23028  {
23029  select_list = node->info.query.q.select.list;
23030  }
23031  else
23032  {
23033  /* It is enough to count the number of select list items from one of the
23034  * union/intersect/difference arguments. If they would be different, this code would not be
23035  * reached, an arg incompatibility is thrown before. Because of the left side recursivity of
23036  * table ops, arg2 is always a PT_SELECT. */
23037  PT_NODE *union_arg2 = node->info.query.q.union_.arg2;
23038  assert (union_arg2->node_type == PT_SELECT);
23039  select_list = union_arg2->info.query.q.select.list;
23040  }
23041 
23042  nb_select_list = pt_length_of_list (select_list);
23043  if (nb_select_list != 1)
23044  {
23045  goto cannot_coerce;
23046  }
23048  wrap_dt->info.data_type.precision,
23049  wrap_dt->info.data_type.dec_precision, wrap_dt,
23050  true) != NO_ERROR)
23051  {
23052  goto cannot_coerce;
23053  }
23054  /* flag PT_EXPR_INFO_CAST_COLL_MODIFIER is not set here; COLLATE modifier is not supported for this
23055  * context */
23056  }
23057  else
23058  {
23059  bool cast_should_fold = false;
23060 
23061  if (node->node_type == PT_VALUE)
23062  {
23063  if (is_string_literal == false && node->data_type
23064  && codeset == node->data_type->info.data_type.units)
23065  {
23066  /* force using COLLATE modifier for VALUEs when codeset is not changed */
23067  use_collate_modifier = true;
23068  }
23069  else
23070  {
23071  cast_should_fold = true;
23072  }
23073  }
23074 
23076  wrap_dt->info.data_type.dec_precision, wrap_dt);
23077  if (node != NULL && use_collate_modifier)
23078  {
23079  assert (node->node_type == PT_EXPR && node->info.expr.op == PT_CAST);
23081  PT_SET_NODE_COLL_MODIFIER (node, coll_id);
23082  }
23083 
23084  if (node != NULL && cast_should_fold)
23085  {
23086  assert (node->node_type == PT_EXPR && node->info.expr.op == PT_CAST);
23088  }
23089  }
23090 
23091  /* 'wrap_dt' is copied in 'pt_wrap_with_cast_op' */
23092  parser_free_node (parser, wrap_dt);
23093  }
23094  else
23095  {
23096  node->data_type->info.data_type.collation_id = coll_id;
23097  node->data_type->info.data_type.units = (int) codeset;
23098  node->data_type->info.data_type.collation_flag = TP_DOMAIN_COLL_NORMAL;
23099  }
23100  }
23101  }
23102  else if (node->expected_domain != NULL && TP_TYPE_HAS_COLLATION (TP_DOMAIN_TYPE (node->expected_domain))
23103  && (TP_DOMAIN_COLLATION (node->expected_domain) != coll_id
23104  || TP_DOMAIN_COLLATION_FLAG (node->expected_domain) == TP_DOMAIN_COLL_LEAVE))
23105  {
23106  TP_DOMAIN *new_domain;
23107 
23108  if (PT_IS_COLLECTION_TYPE (node->type_enum))
23109  {
23110  goto cannot_coerce;
23111  }
23112 
23113  assert (TP_TYPE_HAS_COLLATION (TP_DOMAIN_TYPE (node->expected_domain)));
23114 
23115  if (node->expected_domain->is_cached)
23116  {
23117  /* create new domain */
23118  new_domain = tp_domain_copy (node->expected_domain, false);
23119  new_domain->codeset = (unsigned char) codeset;
23120  new_domain->collation_id = coll_id;
23121  if (node->type_enum == PT_TYPE_MAYBE)
23122  {
23123  if (TP_DOMAIN_COLLATION_FLAG (node->expected_domain) == TP_DOMAIN_COLL_LEAVE)
23124  {
23125  new_domain->collation_flag = TP_DOMAIN_COLL_NORMAL;
23126  }
23127  else
23128  {
23129  new_domain->collation_flag = TP_DOMAIN_COLL_ENFORCE;
23130  }
23131  }
23132 
23133  /* the existing 'expected_domain' may have been created for this specific node and cached, it will remain
23134  * cached */
23135  new_domain = tp_domain_cache (new_domain);
23136  node->expected_domain = new_domain;
23137  }
23138  else
23139  {
23140  /* safe to change the domain directly */
23141  node->expected_domain->codeset = (unsigned char) codeset;
23142  node->expected_domain->collation_id = coll_id;
23143  if (node->type_enum == PT_TYPE_MAYBE)
23144  {
23145  if (TP_DOMAIN_COLLATION_FLAG (node->expected_domain) == TP_DOMAIN_COLL_LEAVE)
23146  {
23147  node->expected_domain->collation_flag = TP_DOMAIN_COLL_NORMAL;
23148  }
23149  else
23150  {
23151  node->expected_domain->collation_flag = TP_DOMAIN_COLL_ENFORCE;
23152  }
23153  }
23154  }
23155  }
23156  else if (node->expected_domain != NULL && TP_IS_SET_TYPE (TP_DOMAIN_TYPE (node->expected_domain)))
23157  {
23158  /* collection domain */
23159  TP_DOMAIN *elem_dom;
23160  TP_DOMAIN *curr_set_dom;
23161  TP_DOMAIN *new_set_dom;
23162  TP_DOMAIN *new_elem_dom;
23163  TP_DOMAIN *save_elem_dom_next;
23164  DB_TYPE exp_db_type;
23165 
23166  /* add domain of string with expected collation */
23167  curr_set_dom = node->expected_domain;
23168  elem_dom = curr_set_dom->setdomain;
23169 
23170  /* copy only parent collection domain */
23171  curr_set_dom->setdomain = NULL;
23172  new_set_dom = tp_domain_copy (curr_set_dom, false);
23173  curr_set_dom->setdomain = elem_dom;
23174  while (elem_dom != NULL)
23175  {
23176  /* create a new domain from this */
23177  save_elem_dom_next = elem_dom->next;
23178  elem_dom->next = NULL;
23179  new_elem_dom = tp_domain_copy (elem_dom, false);
23180  elem_dom->next = save_elem_dom_next;
23181 
23182  if (TP_IS_CHAR_TYPE (TP_DOMAIN_TYPE (elem_dom)))
23183  {
23184  /* for string domains overwrite collation */
23185  new_elem_dom->collation_id = coll_id;
23186  new_elem_dom->codeset = codeset;
23187  }
23188 
23189  tp_domain_add (&(new_set_dom->setdomain), new_elem_dom);
23190  elem_dom = elem_dom->next;
23191  }
23192 
23193  if (PT_IS_CHAR_STRING_TYPE (wrap_type_for_maybe))
23194  {
23195  exp_db_type = pt_type_enum_to_db (wrap_type_for_maybe);
23196  /* create an expected domain to force collation */
23197  new_elem_dom = tp_domain_resolve_default_w_coll (exp_db_type, coll_id, TP_DOMAIN_COLL_ENFORCE);
23198  new_elem_dom = tp_domain_copy (new_elem_dom, false);
23199  tp_domain_add (&(new_set_dom->setdomain), new_elem_dom);
23200  }
23201 
23202  node->expected_domain = tp_domain_cache (new_set_dom);
23203  }
23204 
23205  switch (node->node_type)
23206  {
23207  case PT_VALUE:
23208  if (node->info.value.db_value_is_initialized && node->data_type != NULL)
23209  {
23210  if (PT_IS_COLLECTION_TYPE (node->type_enum))
23211  {
23212  int i;
23213  DB_VALUE *sub_value = NULL;
23214  SETREF *setref = db_get_set (&(node->info.value.db_value));
23215  int set_size = setobj_size (setref->set);
23216 
23217  for (i = 0; i < set_size; i++)
23218  {
23219  setobj_get_element_ptr (setref->set, i, &sub_value);
23220  if (sub_value != NULL && TP_IS_CHAR_TYPE (DB_VALUE_TYPE (sub_value)))
23221  {
23222  db_string_put_cs_and_collation (sub_value, (unsigned char) codeset, coll_id);
23223  }
23224  }
23225  }
23226  else
23227  {
23228  assert (PT_IS_CHAR_STRING_TYPE (node->type_enum));
23229  db_string_put_cs_and_collation (&(node->info.value.db_value), (unsigned char) codeset, coll_id);
23230  }
23231  }
23232  break;
23233  case PT_HOST_VAR:
23234  if (node->expected_domain != NULL)
23235  {
23237  }
23238  else if (node->data_type != NULL && PT_HAS_COLLATION (node->data_type->type_enum))
23239  {
23240  /* this is a HV from an auto-parametrization */
23241  node->data_type->info.data_type.collation_id = coll_id;
23242  node->data_type->info.data_type.units = (int) codeset;
23243  node->data_type->info.data_type.collation_flag = TP_DOMAIN_COLL_ENFORCE;
23244  }
23245  break;
23246  case PT_FUNCTION:
23247  if (preset_hv_in_collection)
23248  {
23249  PT_NODE *arg;
23250  TP_DOMAIN *dom_hv;
23251  DB_TYPE exp_db_type;
23252 
23253  assert (PT_IS_COLLECTION_TYPE (node->type_enum));
23254 
23255  arg = node->info.function.arg_list;
23256  while (arg != NULL)
23257  {
23258  if (arg->node_type != PT_HOST_VAR)
23259  {
23260  arg = arg->next;
23261  continue;
23262  }
23263 
23264  if (arg->expected_domain != NULL)
23265  {
23266  if (!TP_TYPE_HAS_COLLATION (TP_DOMAIN_TYPE (arg->expected_domain))
23267  || TP_DOMAIN_COLLATION (arg->expected_domain) == coll_id)
23268  {
23269  arg = arg->next;
23270  continue;
23271  }
23272 
23273  if (arg->expected_domain->is_cached)
23274  {
23275  /* create new domain */
23276  dom_hv = tp_domain_copy (arg->expected_domain, false);
23277  }
23278  else
23279  {
23280  dom_hv = arg->expected_domain;
23281  }
23282 
23283  dom_hv->codeset = (unsigned char) codeset;
23284  dom_hv->collation_id = coll_id;
23286  }
23287  else
23288  {
23289  assert (PT_IS_CHAR_STRING_TYPE (wrap_type_for_maybe));
23290  exp_db_type = pt_type_enum_to_db (wrap_type_for_maybe);
23291  /* create an expected domain to force collation */
23292  dom_hv = tp_domain_resolve_default_w_coll (exp_db_type, coll_id, TP_DOMAIN_COLL_ENFORCE);
23293  }
23294 
23295  dom_hv = tp_domain_cache (dom_hv);
23296  SET_EXPECTED_DOMAIN (arg, dom_hv);
23298  arg = arg->next;
23299  }
23300  }
23301  break;
23302  case PT_EXPR:
23303  if (is_string_literal == true && node->node_type == PT_EXPR && node->info.expr.op == PT_CAST)
23304  {
23305  PT_NODE *save_next;
23306  /* a PT_VALUE node was wrapped with CAST to change the charset and collation, but the value originated from a
23307  * simple string literal which does not allow COLLATE; this forces a charset conversion and print with the
23308  * new charset introducer, and without COLLATE */
23310  save_next = node->next;
23311  node->next = NULL;
23312  node = pt_fold_const_expr (parser, node, NULL);
23313  if (node != NULL)
23314  {
23315  node->next = save_next;
23316  node->info.value.is_collate_allowed = false;
23317  node->info.value.print_charset = true;
23318  /* print text with new charset */
23319  node->info.value.text = NULL;
23321  }
23322  break;
23323  }
23324  /* special case : CAST */
23325  if (node->info.expr.op == PT_CAST && node->info.expr.cast_type != NULL)
23326  {
23327  /* propagate the collation and codeset to cast */
23328  PT_NODE *cast_type = node->info.expr.cast_type;
23329 
23330  if (PT_IS_COLLECTION_TYPE (cast_type->type_enum))
23331  {
23332  PT_NODE *dt_node = cast_type->data_type;
23333 
23334  assert (dt_node != NULL);
23335 
23336  /* force collation on each collection component */
23337  do
23338  {
23339  if (PT_HAS_COLLATION (dt_node->type_enum))
23340  {
23341  dt_node->info.data_type.collation_id = coll_id;
23342  dt_node->info.data_type.units = (int) codeset;
23343  if ((original_node_type != PT_EXPR) && (!PT_IS_COLLECTION_TYPE (node->type_enum)))
23344  {
23346  }
23347  else
23348  {
23350  }
23351  }
23352 
23353  dt_node = dt_node->next;
23354  }
23355  while (dt_node != NULL);
23356  }
23357  else
23358  {
23359  assert (PT_HAS_COLLATION (cast_type->type_enum));
23360 
23361  cast_type->info.data_type.collation_id = coll_id;
23362  cast_type->info.data_type.units = (int) codeset;
23363 
23364  assert (node->data_type != NULL);
23365  assert (node->data_type->info.data_type.collation_id == coll_id);
23366 
23367  cast_type->info.data_type.collation_flag = node->data_type->info.data_type.collation_flag;
23368  }
23369  }
23370  break;
23371  default:
23372  break;
23373  }
23374 
23375  return node;
23376 
23377 cannot_coerce:
23378  if (codeset != LANG_COERCIBLE_CODESET || !LANG_IS_COERCIBLE_COLL (coll_id))
23379  {
23380  return NULL;
23381  }
23382  return node;
23383 }
23384 
23385 /*
23386  * pt_common_collation () - compute common collation of an operation
23387  *
23388  * return: 0 comon collation and codeset have been detected, -1 otherwise
23389  * arg1_coll_infer(in): collation inference data of arg1
23390  * arg2_coll_infer(in): collation inference data of arg2
23391  * arg3_coll_infer(in): collation inference data of arg3
23392  * args_w_coll(in): how many arguments have collation
23393  * op_has_3_args(in): true operation has 3 arguments
23394  * common_coll(out): common collation detected
23395  * common_cs(out): common codeset detected
23396  *
23397  */
23398 int
23399 pt_common_collation (PT_COLL_INFER * arg1_coll_infer, PT_COLL_INFER * arg2_coll_infer, PT_COLL_INFER * arg3_coll_infer,
23400  const int args_w_coll, bool op_has_3_args, int *common_coll, INTL_CODESET * common_cs)
23401 {
23402 #define MORE_COERCIBLE(arg1_coll_infer, arg2_coll_infer) \
23403  ((((arg1_coll_infer)->can_force_cs) && !((arg2_coll_infer)->can_force_cs)) \
23404  || ((arg1_coll_infer)->coerc_level > (arg2_coll_infer)->coerc_level \
23405  && (arg1_coll_infer)->can_force_cs == (arg2_coll_infer)->can_force_cs))
23406 
23407  assert (common_coll != NULL);
23408  assert (common_cs != NULL);
23409  assert (arg1_coll_infer != NULL);
23410  assert (arg2_coll_infer != NULL);
23411 
23412  if (op_has_3_args)
23413  {
23414  assert (arg3_coll_infer != NULL);
23415  }
23416 
23417  if (arg1_coll_infer->coll_id != arg2_coll_infer->coll_id
23418  && arg1_coll_infer->coerc_level == arg2_coll_infer->coerc_level
23419  && arg1_coll_infer->can_force_cs == arg2_coll_infer->can_force_cs)
23420  {
23421  goto error;
23422  }
23423  else if (MORE_COERCIBLE (arg1_coll_infer, arg2_coll_infer))
23424  {
23425  /* coerce arg1 collation */
23426  if (!INTL_CAN_COERCE_CS (arg1_coll_infer->codeset, arg2_coll_infer->codeset) && !arg1_coll_infer->can_force_cs)
23427  {
23428  goto error;
23429  }
23430  *common_coll = arg2_coll_infer->coll_id;
23431  *common_cs = arg2_coll_infer->codeset;
23432 
23433  /* check arg3 */
23434  if (op_has_3_args && arg3_coll_infer->coll_id != *common_coll)
23435  {
23436  bool set_arg3 = false;
23437 
23438  if (MORE_COERCIBLE (arg2_coll_infer, arg3_coll_infer))
23439  {
23440  set_arg3 = true;
23441  }
23442  else if (MORE_COERCIBLE (arg3_coll_infer, arg2_coll_infer))
23443  {
23444  set_arg3 = false;
23445  }
23446  else
23447  {
23448  goto error;
23449  }
23450 
23451  if (set_arg3)
23452  {
23453  /* coerce to collation of arg3 */
23454  if (!INTL_CAN_COERCE_CS (arg2_coll_infer->codeset, arg3_coll_infer->codeset)
23455  && !arg2_coll_infer->can_force_cs)
23456  {
23457  goto error;
23458  }
23459  if (!INTL_CAN_COERCE_CS (arg1_coll_infer->codeset, arg3_coll_infer->codeset)
23460  && !arg1_coll_infer->can_force_cs)
23461  {
23462  goto error;
23463  }
23464 
23465  *common_coll = arg3_coll_infer->coll_id;
23466  *common_cs = arg3_coll_infer->codeset;
23467  }
23468  else
23469  {
23470  /* coerce arg3 collation */
23471  if (!INTL_CAN_COERCE_CS (arg3_coll_infer->codeset, arg2_coll_infer->codeset)
23472  && !arg3_coll_infer->can_force_cs)
23473  {
23474  goto error;
23475  }
23476 
23477  assert (*common_coll == arg2_coll_infer->coll_id);
23478  assert (*common_cs == arg2_coll_infer->codeset);
23479  }
23480  }
23481  }
23482  else
23483  {
23484  assert (MORE_COERCIBLE (arg2_coll_infer, arg1_coll_infer)
23485  || arg2_coll_infer->coll_id == arg1_coll_infer->coll_id);
23486 
23487  /* coerce arg2 collation */
23488  if (!INTL_CAN_COERCE_CS (arg2_coll_infer->codeset, arg1_coll_infer->codeset) && !arg2_coll_infer->can_force_cs)
23489  {
23490  goto error;
23491  }
23492 
23493  *common_coll = arg1_coll_infer->coll_id;
23494  *common_cs = arg1_coll_infer->codeset;
23495 
23496  /* check arg3 */
23497  if (op_has_3_args && arg3_coll_infer->coll_id != *common_coll)
23498  {
23499  bool set_arg3 = false;
23500  if (MORE_COERCIBLE (arg1_coll_infer, arg3_coll_infer))
23501  {
23502  set_arg3 = true;
23503  }
23504  else if (MORE_COERCIBLE (arg3_coll_infer, arg1_coll_infer))
23505  {
23506  set_arg3 = false;
23507  }
23508  else
23509  {
23510  goto error;
23511  }
23512 
23513  if (set_arg3)
23514  {
23515  /* coerce to collation of arg3 */
23516  if (!INTL_CAN_COERCE_CS (arg1_coll_infer->codeset, arg3_coll_infer->codeset)
23517  && !arg1_coll_infer->can_force_cs)
23518  {
23519  goto error;
23520  }
23521 
23522  if (!INTL_CAN_COERCE_CS (arg2_coll_infer->codeset, arg3_coll_infer->codeset)
23523  && !arg2_coll_infer->can_force_cs)
23524  {
23525  goto error;
23526  }
23527 
23528  *common_coll = arg3_coll_infer->coll_id;
23529  *common_cs = arg3_coll_infer->codeset;
23530  }
23531  else
23532  {
23533  /* coerce arg3 collation */
23534  if (!INTL_CAN_COERCE_CS (arg3_coll_infer->codeset, arg1_coll_infer->codeset)
23535  && !arg3_coll_infer->can_force_cs)
23536  {
23537  goto error;
23538  }
23539 
23540  assert (*common_coll == arg1_coll_infer->coll_id);
23541  assert (*common_cs == arg1_coll_infer->codeset);
23542  }
23543  }
23544  }
23545 
23546  return 0;
23547 
23548 error:
23549 
23550  return -1;
23551 
23552 #undef MORE_COERCIBLE
23553 }
23554 
23555 /*
23556  * pt_check_expr_collation () - checks the collation of an expression node
23557  *
23558  * return: error code
23559  * parser(in): parser context
23560  * node(in): a parse tree expression node
23561  *
23562  */
23563 static int
23564 pt_check_expr_collation (PARSER_CONTEXT * parser, PT_NODE ** node)
23565 {
23566  PT_TYPE_ENUM arg1_type = PT_TYPE_NONE, arg2_type = PT_TYPE_NONE;
23567  PT_TYPE_ENUM arg3_type = PT_TYPE_NONE;
23568  PT_TYPE_ENUM arg1_wrap_type = PT_TYPE_NONE, arg2_wrap_type = PT_TYPE_NONE;
23569  PT_TYPE_ENUM arg3_wrap_type = PT_TYPE_NONE, expr_wrap_type = PT_TYPE_NONE;
23570  PT_TYPE_ENUM arg1_collection_wrap_type = PT_TYPE_NONE, arg2_collection_wrap_type = PT_TYPE_NONE;
23571  PT_OP_TYPE op;
23572  PT_COLL_INFER arg1_coll_inf, arg2_coll_inf, arg3_coll_inf;
23573  PT_NODE *expr = *node;
23574  PT_NODE *arg1, *arg2, *arg3;
23575  PT_NODE *new_node;
23576  int common_coll = LANG_COERCIBLE_COLL;
23578  int args_w_coll_maybe = 0;
23579  int args_having_coll = 0;
23580  bool op_has_3_args;
23581  bool reverse_arg2_arg3;
23582  int expr_coll_modifier = -1;
23583  INTL_CODESET expr_cs_modifier = INTL_CODESET_NONE;
23584  bool use_cast_collate_modifier = false;
23585  bool arg1_need_coerce = false;
23586  bool arg2_need_coerce = false;
23587  bool arg3_need_coerce = false;
23588 
23589  assert (expr != NULL);
23590  assert (expr->node_type == PT_EXPR);
23591 
23592  arg1_coll_inf.coll_id = arg2_coll_inf.coll_id = arg3_coll_inf.coll_id = LANG_COERCIBLE_COLL;
23593  arg1_coll_inf.codeset = arg2_coll_inf.codeset = arg3_coll_inf.codeset = LANG_COERCIBLE_CODESET;
23594  arg1_coll_inf.coerc_level = arg2_coll_inf.coerc_level = arg3_coll_inf.coerc_level = PT_COLLATION_NOT_APPLICABLE;
23595  arg1_coll_inf.can_force_cs = arg2_coll_inf.can_force_cs = arg3_coll_inf.can_force_cs = true;
23596 
23597  /* NULL has no collation */
23598  if (expr->type_enum == PT_TYPE_NULL)
23599  {
23600  return NO_ERROR;
23601  }
23602 
23603  op = expr->info.expr.op;
23604  arg1 = expr->info.expr.arg1;
23605  arg2 = expr->info.expr.arg2;
23606  arg3 = expr->info.expr.arg3;
23607 
23608  expr_coll_modifier = PT_GET_COLLATION_MODIFIER (expr);
23609 
23610  if (expr_coll_modifier != -1)
23611  {
23612  LANG_COLLATION *lc = lang_get_collation (expr_coll_modifier);
23613 
23614  assert (lc != NULL);
23615  expr_cs_modifier = lc->codeset;
23616  }
23617 
23618  pt_fix_arguments_collation_flag (expr);
23619 
23620  if (!pt_is_op_w_collation (op) || (op == PT_PLUS && prm_get_bool_value (PRM_ID_PLUS_AS_CONCAT) == false))
23621  {
23622  if (expr_coll_modifier != -1)
23623  {
23624  if (!(op == PT_EVALUATE_VARIABLE || pt_is_comp_op (op)) && !PT_HAS_COLLATION (expr->type_enum))
23625  {
23627  goto error_exit;
23628  }
23629  goto coerce_result;
23630  }
23631  return NO_ERROR;
23632  }
23633 
23634  op_has_3_args = (op == PT_CONCAT_WS || op == PT_REPLACE || op == PT_TRANSLATE || op == PT_BETWEEN
23635  || op == PT_NOT_BETWEEN || op == PT_IF || op == PT_FIELD || op == PT_INDEX_PREFIX || op == PT_NVL2);
23636  reverse_arg2_arg3 = (op == PT_RPAD || op == PT_LPAD);
23637 
23638  /* step 1 : get info */
23639  if (reverse_arg2_arg3)
23640  {
23641  PT_NODE *tmp = arg2;
23642  arg2 = arg3;
23643  arg3 = tmp;
23644  }
23645 
23646  if (arg1)
23647  {
23648  arg1_type = arg1->type_enum;
23649  }
23650 
23651  if (arg2)
23652  {
23653  arg2_type = arg2->type_enum;
23654  }
23655 
23656  if (arg3)
23657  {
23658  arg3_type = arg3->type_enum;
23659  }
23660 
23661  /* will not check collation for BETWEEN op when arg1 does not have collation or arg2 is a range expression */
23662  if ((op == PT_BETWEEN || op == PT_NOT_BETWEEN)
23663  && (!(PT_HAS_COLLATION (arg1_type) || arg1_type == PT_TYPE_MAYBE)
23664  || (arg2 && arg2->node_type == PT_EXPR && pt_is_between_range_op (arg2->info.expr.op))))
23665  {
23666  return NO_ERROR;
23667  }
23668 
23669  if (op == PT_MINUS && (!PT_IS_COLLECTION_TYPE (arg1_type) && !PT_IS_COLLECTION_TYPE (arg2_type))
23670  && (arg1_type != PT_TYPE_MAYBE || arg2_type != PT_TYPE_MAYBE))
23671  {
23672  return NO_ERROR;
23673  }
23674 
23675  if (PT_HAS_COLLATION (arg1_type)
23676  || (arg1_type == PT_TYPE_MAYBE
23677  && (arg1->expected_domain == NULL || !TP_IS_SET_TYPE (TP_DOMAIN_TYPE (arg1->expected_domain)))))
23678  {
23679  if (pt_get_collation_info (arg1, &arg1_coll_inf))
23680  {
23681  args_w_coll_maybe++;
23682  }
23683 
23684  if (arg1_coll_inf.can_force_cs == false)
23685  {
23686  args_having_coll++;
23687  common_coll = arg1_coll_inf.coll_id;
23688  common_cs = arg1_coll_inf.codeset;
23689  }
23690  else
23691  {
23692  arg1_need_coerce = true;
23693  }
23694  }
23695  else if (PT_IS_COLLECTION_TYPE (arg1_type)
23696  || (arg1_type == PT_TYPE_MAYBE && TP_IS_SET_TYPE (TP_DOMAIN_TYPE (arg1->expected_domain))))
23697  {
23698  int status = pt_get_collation_info_for_collection_type (parser, arg1,
23699  &arg1_coll_inf);
23700 
23701  if (status == ERROR_COLLATION)
23702  {
23703  goto error_exit;
23704  }
23705  else if (status == HAS_COLLATION)
23706  {
23707  args_w_coll_maybe++;
23708  if (arg1_coll_inf.can_force_cs == false)
23709  {
23710  args_having_coll++;
23711  common_coll = arg1_coll_inf.coll_id;
23712  common_cs = arg1_coll_inf.codeset;
23713  }
23714  else
23715  {
23716  arg1_need_coerce = true;
23717  }
23718  }
23719  }
23720 
23721  if (PT_HAS_COLLATION (arg2_type)
23722  || (arg2_type == PT_TYPE_MAYBE
23723  && (arg2->expected_domain == NULL || !TP_IS_SET_TYPE (TP_DOMAIN_TYPE (arg2->expected_domain)))))
23724  {
23725  if (pt_get_collation_info (arg2, &arg2_coll_inf))
23726  {
23727  args_w_coll_maybe++;
23728  }
23729 
23730  if (arg2_coll_inf.can_force_cs == false)
23731  {
23732  args_having_coll++;
23733  common_coll = arg2_coll_inf.coll_id;
23734  common_cs = arg2_coll_inf.codeset;
23735  }
23736  else
23737  {
23738  arg2_need_coerce = true;
23739  }
23740  }
23741  else if (PT_IS_COLLECTION_TYPE (arg2_type)
23742  || (arg2_type == PT_TYPE_MAYBE && TP_IS_SET_TYPE (TP_DOMAIN_TYPE (arg2->expected_domain))))
23743  {
23744  int status = pt_get_collation_info_for_collection_type (parser, arg2,
23745  &arg2_coll_inf);
23746 
23747  if (status == ERROR_COLLATION)
23748  {
23749  goto error_exit;
23750  }
23751  else if (status == HAS_COLLATION)
23752  {
23753  args_w_coll_maybe++;
23754  if (arg2_coll_inf.can_force_cs == false)
23755  {
23756  args_having_coll++;
23757  common_coll = arg2_coll_inf.coll_id;
23758  common_cs = arg2_coll_inf.codeset;
23759  }
23760  else
23761  {
23762  arg2_need_coerce = true;
23763  }
23764  }
23765  }
23766 
23767  if (op_has_3_args)
23768  {
23769  if (PT_HAS_COLLATION (arg3_type) || arg3_type == PT_TYPE_MAYBE)
23770  {
23771  if (pt_get_collation_info (arg3, &arg3_coll_inf))
23772  {
23773  args_w_coll_maybe++;
23774  }
23775 
23776  if (arg3_coll_inf.can_force_cs == false)
23777  {
23778  args_having_coll++;
23779  common_coll = arg3_coll_inf.coll_id;
23780  common_cs = arg3_coll_inf.codeset;
23781  }
23782  else
23783  {
23784  arg3_need_coerce = true;
23785  }
23786  }
23787  else if (PT_IS_COLLECTION_TYPE (arg3_type))
23788  {
23789  int status = pt_get_collation_info_for_collection_type (parser, arg3,
23790  &arg3_coll_inf);
23791 
23792  if (status == ERROR_COLLATION)
23793  {
23794  goto error_exit;
23795  }
23796  else if (status == HAS_COLLATION)
23797  {
23798  args_w_coll_maybe++;
23799  if (arg3_coll_inf.can_force_cs == false)
23800  {
23801  args_having_coll++;
23802  common_coll = arg3_coll_inf.coll_id;
23803  common_cs = arg3_coll_inf.codeset;
23804  }
23805  }
23806  }
23807  }
23808 
23809  if (expr_coll_modifier != -1 && pt_is_comp_op (op) && args_w_coll_maybe > 0)
23810  {
23811  /* for comparisons, force the collation of each argument to have the collation of expression */
23812  if (PT_HAS_COLLATION (arg1_type) && arg1_coll_inf.codeset != expr_cs_modifier)
23813  {
23815  lang_get_codeset_name (arg1_coll_inf.codeset), lang_get_codeset_name (expr_cs_modifier));
23816  goto error_exit;
23817  }
23818  if (PT_HAS_COLLATION (arg2_type) && arg2_coll_inf.codeset != expr_cs_modifier)
23819  {
23821  lang_get_codeset_name (arg2_coll_inf.codeset), lang_get_codeset_name (expr_cs_modifier));
23822  goto error_exit;
23823  }
23824  if (PT_HAS_COLLATION (arg3_type) && arg3_coll_inf.codeset != expr_cs_modifier)
23825  {
23827  lang_get_codeset_name (arg3_coll_inf.codeset), lang_get_codeset_name (expr_cs_modifier));
23828  goto error_exit;
23829  }
23830 
23831  common_cs = expr_cs_modifier;
23832  common_coll = expr_coll_modifier;
23833  use_cast_collate_modifier = true;
23834  goto coerce_arg;
23835  }
23836 
23837  if (args_w_coll_maybe <= 1)
23838  {
23839  goto coerce_result;
23840  }
23841 
23842  /* step 2 : compute collation to use */
23843  assert (args_w_coll_maybe >= 2);
23844 
23845  if (op_has_3_args)
23846  {
23847  if (args_w_coll_maybe < 3)
23848  {
23849  if (!(PT_HAS_COLLATION (arg1_type) || arg1_type == PT_TYPE_MAYBE || PT_IS_COLLECTION_TYPE (arg1_type)))
23850  {
23851  arg1_coll_inf.coll_id = common_coll;
23852  arg1_coll_inf.codeset = common_cs;
23853  arg1_coll_inf.coerc_level = PT_COLLATION_NOT_APPLICABLE;
23854  }
23855 
23856  if (!(PT_HAS_COLLATION (arg2_type) || arg2_type == PT_TYPE_MAYBE || PT_IS_COLLECTION_TYPE (arg2_type)))
23857  {
23858  arg2_coll_inf.coll_id = common_coll;
23859  arg2_coll_inf.codeset = common_cs;
23860  arg2_coll_inf.coerc_level = PT_COLLATION_NOT_APPLICABLE;
23861  }
23862 
23863  if (!(PT_HAS_COLLATION (arg3_type) || arg3_type == PT_TYPE_MAYBE || PT_IS_COLLECTION_TYPE (arg3_type)))
23864  {
23865  arg3_coll_inf.coll_id = common_coll;
23866  arg3_coll_inf.codeset = common_cs;
23867  arg3_coll_inf.coerc_level = PT_COLLATION_NOT_APPLICABLE;
23868  }
23869  }
23870 
23871  if (arg1_coll_inf.coll_id == arg2_coll_inf.coll_id && arg2_coll_inf.coll_id == arg3_coll_inf.coll_id
23872  && (arg1_type != PT_TYPE_MAYBE && arg2_type != PT_TYPE_MAYBE && arg2_type != PT_TYPE_MAYBE)
23873  && (arg1_need_coerce == false && arg2_need_coerce == false && arg3_need_coerce == false))
23874  {
23875  assert (arg1_coll_inf.codeset == arg2_coll_inf.codeset && arg2_coll_inf.codeset == arg3_coll_inf.codeset);
23876  goto coerce_result;
23877  }
23878  }
23879  else
23880  {
23881  if (arg1_coll_inf.coll_id == arg2_coll_inf.coll_id && (arg1_type != PT_TYPE_MAYBE && arg2_type != PT_TYPE_MAYBE)
23882  && (arg1_need_coerce == false && arg2_need_coerce == false))
23883  {
23884  assert (arg1_coll_inf.codeset == arg2_coll_inf.codeset);
23885  goto coerce_result;
23886  }
23887  }
23888 
23889  assert (arg1_coll_inf.coll_id != arg2_coll_inf.coll_id || arg1_coll_inf.coll_id != arg3_coll_inf.coll_id
23890  || arg1_type == PT_TYPE_MAYBE || arg2_type == PT_TYPE_MAYBE || arg3_type == PT_TYPE_MAYBE
23891  || arg1_need_coerce == true || arg2_need_coerce == true || arg3_need_coerce == true);
23892 
23893  if (pt_common_collation (&arg1_coll_inf, &arg2_coll_inf, &arg3_coll_inf, args_w_coll_maybe, op_has_3_args,
23894  &common_coll, &common_cs) != 0)
23895  {
23896  goto error;
23897  }
23898 
23899 coerce_arg:
23900  /* step 3 : coerce collation of expression arguments */
23901  if (((arg1_type == PT_TYPE_MAYBE || arg1_need_coerce) && args_having_coll > 0)
23902  || (common_coll != arg1_coll_inf.coll_id && (PT_HAS_COLLATION (arg1_type) || PT_IS_COLLECTION_TYPE (arg1_type))))
23903  {
23904  if (arg1_type == PT_TYPE_MAYBE || PT_IS_COLLECTION_TYPE (arg1_type))
23905  {
23906  arg1_wrap_type = pt_wrap_type_for_collation (arg1, arg2, arg3, &arg1_collection_wrap_type);
23907  }
23908  else
23909  {
23910  arg1_wrap_type = PT_TYPE_NONE;
23911  }
23912 
23913  new_node =
23914  pt_coerce_node_collation (parser, arg1, common_coll, common_cs, arg1_coll_inf.can_force_cs,
23915  use_cast_collate_modifier, PT_COLL_WRAP_TYPE_FOR_MAYBE (arg1_wrap_type),
23916  arg1_collection_wrap_type);
23917 
23918  if (new_node == NULL)
23919  {
23920  goto error;
23921  }
23922 
23923  expr->info.expr.arg1 = new_node;
23924  }
23925 
23926  if (((arg2_type == PT_TYPE_MAYBE || arg2_need_coerce) && args_having_coll > 0)
23927  || (common_coll != arg2_coll_inf.coll_id && (PT_HAS_COLLATION (arg2_type) || PT_IS_COLLECTION_TYPE (arg2_type))))
23928  {
23929  if (arg2_type == PT_TYPE_MAYBE || PT_IS_COLLECTION_TYPE (arg2_type))
23930  {
23931  arg2_wrap_type = pt_wrap_type_for_collation (arg1, arg2, arg3, &arg1_collection_wrap_type);
23932  }
23933  else
23934  {
23935  arg2_wrap_type = PT_TYPE_NONE;
23936  }
23937 
23938  new_node =
23939  pt_coerce_node_collation (parser, arg2, common_coll, common_cs, arg2_coll_inf.can_force_cs,
23940  use_cast_collate_modifier, PT_COLL_WRAP_TYPE_FOR_MAYBE (arg2_wrap_type),
23941  arg2_collection_wrap_type);
23942 
23943  if (new_node == NULL)
23944  {
23945  goto error;
23946  }
23947 
23948  if (reverse_arg2_arg3)
23949  {
23950  expr->info.expr.arg3 = new_node;
23951  }
23952  else
23953  {
23954  expr->info.expr.arg2 = new_node;
23955  }
23956  }
23957 
23958  if (op_has_3_args
23959  && (((arg3_type == PT_TYPE_MAYBE || arg3_need_coerce) && args_having_coll > 0)
23960  || (common_coll != arg3_coll_inf.coll_id && PT_HAS_COLLATION (arg3_type))))
23961  {
23962  if (arg3_type == PT_TYPE_MAYBE)
23963  {
23964  arg3_wrap_type = pt_wrap_type_for_collation (arg1, arg2, arg3, NULL);
23965  }
23966  else
23967  {
23968  arg3_wrap_type = PT_TYPE_NONE;
23969  }
23970 
23971  new_node =
23972  pt_coerce_node_collation (parser, arg3, common_coll, common_cs, arg3_coll_inf.can_force_cs,
23973  use_cast_collate_modifier, PT_COLL_WRAP_TYPE_FOR_MAYBE (arg3_wrap_type),
23974  PT_TYPE_NONE);
23975 
23976  if (new_node == NULL)
23977  {
23978  goto error;
23979  }
23980 
23981  expr->info.expr.arg3 = new_node;
23982  }
23983 
23984  /* step 4: update collation of expression result */
23985 coerce_result:
23986  if (op == PT_CHR || op == PT_CLOB_TO_CHAR)
23987  {
23988  /* for these operators, we don't want the arguments' collations to infere common collation, but special values of
23989  * arg2 */
23990  common_cs = (INTL_CODESET) expr->data_type->info.data_type.units;
23991  common_coll = expr->data_type->info.data_type.collation_id;
23992  }
23993 
23994  if (expr_coll_modifier != -1 && expr->data_type != NULL)
23995  {
23996  if (expr_cs_modifier != common_cs)
23997  {
23999  lang_get_codeset_name (common_cs), lang_get_codeset_name (expr_cs_modifier));
24000  goto error_exit;
24001  }
24002 
24003  common_coll = expr_coll_modifier;
24004  common_cs = expr_cs_modifier;
24005  }
24006 
24007  switch (op)
24008  {
24009  case PT_COALESCE:
24010  case PT_NVL:
24011  case PT_NVL2:
24012  case PT_IFNULL:
24013  case PT_GREATEST:
24014  case PT_LEAST:
24015  case PT_NULLIF:
24016  if (expr->is_wrapped_res_for_coll)
24017  {
24018  break;
24019  }
24020  if (expr->type_enum == PT_TYPE_MAYBE && args_having_coll > 0)
24021  {
24022  assert (args_w_coll_maybe > 0);
24023  if (op == PT_NVL2)
24024  {
24025  if (PT_HAS_COLLATION (arg1_type))
24026  {
24027  expr_wrap_type = arg1_type;
24028  }
24029  else if (PT_HAS_COLLATION (arg2_type))
24030  {
24031  expr_wrap_type = arg2_type;
24032  }
24033  else
24034  {
24035  expr_wrap_type = arg3_type;
24036  }
24037  }
24038  else
24039  {
24040  if (PT_HAS_COLLATION (arg1_type))
24041  {
24042  expr_wrap_type = arg1_type;
24043  }
24044  else
24045  {
24046  expr_wrap_type = arg2_type;
24047  }
24048  }
24049 
24050  assert (PT_HAS_COLLATION (expr_wrap_type));
24051 
24052  new_node =
24053  pt_coerce_node_collation (parser, expr, common_coll, common_cs, true, false,
24054  PT_COLL_WRAP_TYPE_FOR_MAYBE (expr_wrap_type), PT_TYPE_NONE);
24055 
24056  expr->is_wrapped_res_for_coll = 1;
24057  if (new_node == NULL)
24058  {
24059  goto error;
24060  }
24061 
24062  expr = new_node;
24063  break;
24064  }
24065  /* fall through */
24066  case PT_PLUS:
24067  if (expr->type_enum == PT_TYPE_MAYBE)
24068  {
24069  if (args_having_coll == 0)
24070  {
24071  break;
24072  }
24073  }
24074  else if (!PT_HAS_COLLATION (expr->type_enum))
24075  {
24076  break;
24077  }
24078  /* fall through */
24079  case PT_CONCAT:
24080  case PT_CONCAT_WS:
24081  case PT_RPAD:
24082  case PT_LPAD:
24083  case PT_SUBSTRING:
24084  case PT_MID:
24085  case PT_REPLACE:
24086  case PT_TRANSLATE:
24087  case PT_STRCAT:
24088  case PT_DATE_FORMAT:
24089  case PT_TIME_FORMAT:
24090  case PT_LOWER:
24091  case PT_UPPER:
24092  case PT_REPEAT:
24093  case PT_RTRIM:
24094  case PT_LTRIM:
24095  case PT_TRIM:
24096  case PT_LEFT:
24097  case PT_RIGHT:
24098  case PT_SUBSTRING_INDEX:
24099  case PT_DATEF:
24100  case PT_TIMEF:
24101  case PT_IF:
24102  case PT_REVERSE:
24103  case PT_CONNECT_BY_ROOT:
24104  case PT_PRIOR:
24105  case PT_QPRIOR:
24106  case PT_INDEX_PREFIX:
24107  if (args_having_coll > 0)
24108  {
24109  if (expr->type_enum == PT_TYPE_MAYBE)
24110  {
24111  if (expr->is_wrapped_res_for_coll)
24112  {
24113  break;
24114  }
24115  expr_wrap_type = pt_wrap_type_for_collation (arg1, arg2, arg3, NULL);
24116  expr->is_wrapped_res_for_coll = 1;
24117  }
24118  else
24119  {
24120  expr_wrap_type = PT_TYPE_NONE;
24121  }
24122 
24123  new_node =
24124  pt_coerce_node_collation (parser, expr, common_coll, common_cs, true, false,
24125  PT_COLL_WRAP_TYPE_FOR_MAYBE (expr_wrap_type), PT_TYPE_NONE);
24126  if (new_node == NULL)
24127  {
24128  goto error;
24129  }
24130 
24131  expr = new_node;
24132  }
24133  break;
24134  default:
24135  break;
24136  }
24137 
24138  *node = expr;
24139 
24140  return NO_ERROR;
24141 
24142 error:
24144 
24145 error_exit:
24146  return ER_FAILED;
24147 }
24148 
24149 /*
24150  * pt_check_recursive_expr_collation () - checks the collation of a recursive
24151  * expression node
24152  *
24153  * return: error code
24154  * parser(in): parser context
24155  * node(in): a parse tree expression node
24156  *
24157  */
24158 static int
24159 pt_check_recursive_expr_collation (PARSER_CONTEXT * parser, PT_NODE ** node)
24160 {
24161  PT_NODE *expr = *node;
24162  PT_OP_TYPE op;
24163  int recurs_coll = -1;
24164  INTL_CODESET recurs_cs = INTL_CODESET_NONE;
24165  PT_COLL_COERC_LEV recurs_coerc_level = PT_COLLATION_NOT_APPLICABLE;
24166  bool need_arg_coerc = false;
24167 
24168  assert (expr != NULL);
24169 
24170  op = expr->info.expr.op;
24171  assert (op == PT_DECODE || op == PT_CASE);
24172 
24173  while (PT_IS_RECURSIVE_EXPRESSION (expr) && op == expr->info.expr.op)
24174  {
24175  PT_NODE *arg1 = expr->info.expr.arg1;
24176  PT_NODE *arg2 = expr->info.expr.arg2;
24177  PT_COLL_INFER arg1_coll_infer, arg2_coll_infer;
24178 
24179  arg1_coll_infer.coll_id = arg2_coll_infer.coll_id = -1;
24180 
24181  if (pt_get_collation_info (arg1, &arg1_coll_infer))
24182  {
24183  if (recurs_coll != -1 && recurs_coll != arg1_coll_infer.coll_id
24184  && recurs_coerc_level == arg1_coll_infer.coerc_level)
24185  {
24186  goto error;
24187  }
24188  else
24189  {
24190  if (recurs_coll != -1)
24191  {
24192  need_arg_coerc = true;
24193  }
24194 
24195  if (recurs_coerc_level > arg1_coll_infer.coerc_level || recurs_coll == -1)
24196  {
24197  recurs_coerc_level = arg1_coll_infer.coerc_level;
24198  recurs_coll = arg1_coll_infer.coll_id;
24199  recurs_cs = arg1_coll_infer.codeset;
24200  }
24201  }
24202  }
24203 
24204  if (arg2 != NULL && (arg2->node_type != PT_EXPR || op != arg2->info.expr.op)
24205  && pt_get_collation_info (arg2, &arg2_coll_infer))
24206  {
24207  if (recurs_coll != -1 && recurs_coll != arg2_coll_infer.coll_id
24208  && recurs_coerc_level == arg2_coll_infer.coerc_level)
24209  {
24210  goto error;
24211  }
24212  else
24213  {
24214  if (recurs_coll != -1)
24215  {
24216  need_arg_coerc = true;
24217  }
24218 
24219  if (recurs_coerc_level > arg2_coll_infer.coerc_level || recurs_coll == -1)
24220  {
24221  recurs_coerc_level = arg2_coll_infer.coerc_level;
24222  recurs_coll = arg2_coll_infer.coll_id;
24223  recurs_cs = arg2_coll_infer.codeset;
24224  }
24225  }
24226  }
24227 
24229  expr = arg2;
24230  }
24231 
24232  expr = *node;
24233  while (need_arg_coerc && PT_IS_RECURSIVE_EXPRESSION (expr) && op == expr->info.expr.op)
24234  {
24235  PT_NODE *arg1 = expr->info.expr.arg1;
24236  PT_NODE *arg2 = expr->info.expr.arg2;
24237  PT_COLL_INFER arg1_coll_infer, arg2_coll_infer;
24238 
24239  arg1_coll_infer.coll_id = arg2_coll_infer.coll_id = -1;
24240 
24241  if (PT_HAS_COLLATION (arg1->type_enum) || arg1->type_enum == PT_TYPE_MAYBE)
24242  {
24243  (void) pt_get_collation_info (arg1, &arg1_coll_infer);
24244  }
24245 
24246  if ((PT_HAS_COLLATION (arg1->type_enum) && arg1_coll_infer.coll_id != recurs_coll)
24247  || arg1->type_enum == PT_TYPE_MAYBE)
24248  {
24249  arg1 =
24250  pt_coerce_node_collation (parser, arg1, recurs_coll, recurs_cs, arg1_coll_infer.can_force_cs, false,
24252  if (arg1 == NULL)
24253  {
24254  goto error;
24255  }
24256  expr->info.expr.arg1 = arg1;
24257  }
24258 
24259  if (arg2 != NULL)
24260  {
24261  if (arg2->node_type != PT_EXPR || op != arg2->info.expr.op)
24262  {
24263  if (PT_HAS_COLLATION (arg2->type_enum) || arg2->type_enum == PT_TYPE_MAYBE)
24264  {
24265  (void) pt_get_collation_info (arg2, &arg2_coll_infer);
24266  }
24267 
24268  if ((PT_HAS_COLLATION (arg2->type_enum) && arg2_coll_infer.coll_id != recurs_coll)
24269  || arg2->type_enum == PT_TYPE_MAYBE)
24270  {
24271  arg2 =
24272  pt_coerce_node_collation (parser, arg2, recurs_coll, recurs_cs, arg2_coll_infer.can_force_cs, false,
24274  if (arg2 == NULL)
24275  {
24276  goto error;
24277  }
24278  expr->info.expr.arg2 = arg2;
24279  }
24280  }
24281  else if (arg2->node_type == PT_EXPR && op == arg2->info.expr.op && PT_HAS_COLLATION (arg2->type_enum))
24282  {
24283  /* force collation on recursive expression node */
24284  arg2 =
24285  pt_coerce_node_collation (parser, arg2, recurs_coll, recurs_cs, true, false,
24287  if (arg2 == NULL)
24288  {
24289  goto error;
24290  }
24291  expr->info.expr.arg2 = arg2;
24292  }
24293  }
24294 
24296  expr = arg2;
24297  }
24298 
24299  expr = *node;
24300  if (recurs_coll != -1 && PT_HAS_COLLATION (expr->type_enum))
24301  {
24302  *node =
24303  pt_coerce_node_collation (parser, expr, recurs_coll, recurs_cs, true, false,
24305 
24306  if (*node == NULL)
24307  {
24308  goto error;
24309  }
24310  }
24311 
24312  return NO_ERROR;
24313 
24314 error:
24316 
24317  return ER_FAILED;
24318 }
24319 
24320 /*
24321 * pt_node_to_enumeration_expr () - wrap node with PT_TO_ENUMERATION_VALUE
24322 * expression
24323 * return : new node or null
24324 * parser (in) :
24325 * data_type (in) :
24326 * node (in) :
24327 */
24328 static PT_NODE *
24329 pt_node_to_enumeration_expr (PARSER_CONTEXT * parser, PT_NODE * data_type, PT_NODE * node)
24330 {
24331  PT_NODE *expr = NULL;
24332  if (parser == NULL || data_type == NULL || node == NULL)
24333  {
24334  assert (false);
24335  return NULL;
24336  }
24337 
24338  if (PT_HAS_COLLATION (node->type_enum) && node->data_type != NULL)
24339  {
24340  if (!INTL_CAN_COERCE_CS (node->data_type->info.data_type.units, data_type->info.data_type.units))
24341  {
24344  return node;
24345  }
24346  }
24347 
24348  expr = parser_new_node (parser, PT_EXPR);
24349  if (expr == NULL)
24350  {
24352  return NULL;
24353  }
24354 
24355  expr->info.expr.arg1 = node;
24357  expr->data_type = parser_copy_tree (parser, data_type);
24359  return expr;
24360 }
24361 
24362 /*
24363 * pt_select_list_to_enumeration_expr () - wrap select list with
24364 * PT_TO_ENUMERATION_VALUE expression
24365 * return : new node or null
24366 * parser (in) :
24367 * data_type (in) :
24368 * node (in) :
24369 */
24370 static PT_NODE *
24371 pt_select_list_to_enumeration_expr (PARSER_CONTEXT * parser, PT_NODE * data_type, PT_NODE * node)
24372 {
24373  PT_NODE *new_node = NULL;
24374 
24375  if (node == NULL || data_type == NULL)
24376  {
24377  return node;
24378  }
24379 
24380  if (!PT_IS_QUERY_NODE_TYPE (node->node_type))
24381  {
24382  return node;
24383  }
24384  switch (node->node_type)
24385  {
24386  case PT_SELECT:
24387  {
24388  PT_NODE *item = NULL;
24389  PT_NODE *prev = NULL;
24390  PT_NODE *select_list = node->info.query.q.select.list;
24391  for (item = select_list; item != NULL; prev = item, item = item->next)
24392  {
24393  if (item->type_enum == PT_TYPE_ENUMERATION)
24394  {
24395  /* nothing to do here */
24396  continue;
24397  }
24398  new_node = pt_node_to_enumeration_expr (parser, data_type, item);
24399  if (new_node == NULL)
24400  {
24401  return NULL;
24402  }
24403  new_node->next = item->next;
24404  item->next = NULL;
24405  item = new_node;
24406  /* first node in the list */
24407  if (prev == NULL)
24408  {
24409  node->info.query.q.select.list = item;
24410  }
24411  else
24412  {
24413  prev->next = item;
24414  }
24415  }
24416  break;
24417  }
24418  case PT_DIFFERENCE:
24419  case PT_INTERSECTION:
24420  case PT_UNION:
24421  new_node = pt_select_list_to_enumeration_expr (parser, data_type, node->info.query.q.union_.arg1);
24422  if (new_node == NULL)
24423  {
24424  return NULL;
24425  }
24426  node->info.query.q.union_.arg1 = new_node;
24427  new_node = pt_select_list_to_enumeration_expr (parser, data_type, node->info.query.q.union_.arg2);
24428  if (new_node == NULL)
24429  {
24430  return NULL;
24431  }
24432  node->info.query.q.union_.arg2 = new_node;
24433  break;
24434  default:
24435  break;
24436  }
24437  return node;
24438 }
24439 
24440 /*
24441 * pt_is_enumeration_special_comparison () - check if the comparison is a
24442 * '=' comparison that involves ENUM types and constants or if it's a IN
24443 * 'IN' comparison in which the left operator is an ENUM.
24444 * return : true if it is a special ENUM comparison or false otherwise.
24445 * arg1 (in) : left argument
24446 * op (in) : expression operator
24447 * arg2 (in) : right argument
24448 */
24449 static bool
24450 pt_is_enumeration_special_comparison (PT_NODE * arg1, PT_OP_TYPE op, PT_NODE * arg2)
24451 {
24452  PT_NODE *arg_tmp = NULL;
24453 
24454  if (arg1 == NULL || arg2 == NULL)
24455  {
24456  return false;
24457  }
24458 
24459  switch (op)
24460  {
24461  case PT_EQ:
24462  case PT_NE:
24463  case PT_NULLSAFE_EQ:
24464  if (arg1->type_enum != PT_TYPE_ENUMERATION)
24465  {
24466  if (arg2->type_enum != PT_TYPE_ENUMERATION)
24467  {
24468  return false;
24469  }
24470 
24471  arg_tmp = arg1;
24472  arg1 = arg2;
24473  arg2 = arg_tmp;
24474  }
24475  else if (arg2->type_enum == PT_TYPE_ENUMERATION && arg1->data_type != NULL && arg2->data_type != NULL)
24476  {
24477  if (pt_is_same_enum_data_type (arg1->data_type, arg2->data_type))
24478  {
24479  return true;
24480  }
24481  }
24482  if (arg2->node_type == PT_EXPR)
24483  {
24484  if (arg2->info.expr.op != PT_TO_ENUMERATION_VALUE)
24485  {
24486  return false;
24487  }
24488  }
24489  else
24490  {
24491  if (!PT_IS_CONST (arg2))
24492  {
24493  return false;
24494  }
24495  }
24496  return true;
24497  case PT_IS_IN:
24498  case PT_IS_NOT_IN:
24499  case PT_EQ_SOME:
24500  case PT_NE_SOME:
24501  case PT_EQ_ALL:
24502  case PT_NE_ALL:
24503  return (arg1->type_enum == PT_TYPE_ENUMERATION);
24504  default:
24505  return false;
24506  }
24507 }
24508 
24509 /*
24510 * pt_fix_enumeration_comparison () - fix comparisons for enumeration type
24511 * return : modified node or NULL
24512 * parser (in) :
24513 * expr (in) :
24514 */
24515 static PT_NODE *
24516 pt_fix_enumeration_comparison (PARSER_CONTEXT * parser, PT_NODE * expr)
24517 {
24518  PT_NODE *arg1 = NULL, *arg2 = NULL;
24519  PT_NODE *node = NULL, *save_next = NULL;
24520  PT_NODE *list = NULL, *list_prev = NULL, **list_start = NULL;
24521  PT_OP_TYPE op;
24522  if (expr == NULL || expr->node_type != PT_EXPR)
24523  {
24524  return expr;
24525  }
24526  op = expr->info.expr.op;
24527  arg1 = expr->info.expr.arg1;
24528  arg2 = expr->info.expr.arg2;
24529 
24530  switch (op)
24531  {
24532  case PT_EQ:
24533  case PT_NE:
24534  case PT_NULLSAFE_EQ:
24535  if (PT_IS_CONST (arg1))
24536  {
24537  if (PT_IS_CONST (arg2))
24538  {
24539  /* const op const does not need special handling */
24540  return expr;
24541  }
24542  /* switch arg1 with arg2 so that we have non cost operand on the left side */
24543  node = arg1;
24544  arg1 = arg2;
24545  arg2 = node;
24546  }
24547 
24548  if (arg1->type_enum != PT_TYPE_ENUMERATION || !PT_IS_CONST (arg2))
24549  {
24550  /* we're only handling enumeration comp const */
24551  return expr;
24552  }
24553  if (pt_is_same_enum_data_type (arg1->data_type, arg2->data_type))
24554  {
24555  return expr;
24556  }
24557 
24558  if (arg2->type_enum == PT_TYPE_ENUMERATION && arg2->data_type != NULL)
24559  {
24560  TP_DOMAIN *domain = pt_data_type_to_db_domain (parser, arg2->data_type, NULL);
24561  DB_VALUE *dbval = pt_value_to_db (parser, arg2);
24562 
24563  if (domain == NULL)
24564  {
24565  return NULL;
24566  }
24567  if (dbval != NULL
24568  && ((db_get_enum_string (dbval) == NULL && db_get_enum_short (dbval) == 0)
24569  || ((db_get_enum_string (dbval) != NULL && db_get_enum_short (dbval) > 0)
24570  && tp_domain_select (domain, dbval, 0, TP_EXACT_MATCH) != NULL)))
24571  {
24572  return expr;
24573  }
24574  }
24575  break;
24576  case PT_IS_IN:
24577  case PT_IS_NOT_IN:
24578  case PT_EQ_SOME:
24579  case PT_NE_SOME:
24580  case PT_EQ_ALL:
24581  case PT_NE_ALL:
24582  break;
24583  default:
24584  return expr;
24585  }
24586 
24587  if (arg1->data_type == NULL || arg1->data_type->info.data_type.enumeration == NULL)
24588  {
24589  /* we don't know the actual enumeration type */
24590  return expr;
24591  }
24592 
24593  switch (op)
24594  {
24595  case PT_EQ:
24596  case PT_NE:
24597  case PT_NULLSAFE_EQ:
24598  node = pt_node_to_enumeration_expr (parser, arg1->data_type, arg2);
24599  if (node == NULL)
24600  {
24601  return NULL;
24602  }
24603  arg2 = node;
24604  break;
24605  case PT_IS_IN:
24606  case PT_IS_NOT_IN:
24607  case PT_EQ_SOME:
24608  case PT_NE_SOME:
24609  case PT_EQ_ALL:
24610  case PT_NE_ALL:
24611  if (PT_IS_QUERY_NODE_TYPE (arg2->node_type))
24612  {
24613  node = pt_select_list_to_enumeration_expr (parser, arg1->data_type, arg2);
24614  if (node == NULL)
24615  {
24616  return NULL;
24617  }
24618  arg2 = node;
24619  break;
24620  }
24621  /* not a subquery */
24622  switch (arg2->node_type)
24623  {
24624  case PT_VALUE:
24625  assert (PT_IS_COLLECTION_TYPE (arg2->type_enum) || arg2->type_enum == PT_TYPE_EXPR_SET);
24626  /* convert this value to a multiset */
24628  if (node == NULL)
24629  {
24631  return NULL;
24632  }
24633  node->info.function.function_type = F_SET;
24634  node->info.function.arg_list = arg2->info.value.data_value.set;
24635  node->type_enum = arg2->type_enum;
24636 
24637  arg2->info.value.data_value.set = NULL;
24638  parser_free_tree (parser, arg2);
24639  arg2 = node;
24640 
24641  /* fall through */
24642 
24643  case PT_FUNCTION:
24644  list = arg2->info.function.arg_list;
24645  list_start = &arg2->info.function.arg_list;
24646  break;
24647 
24648  default:
24649  return expr;
24650  }
24651 
24652  while (list != NULL)
24653  {
24654  /* Skip nodes that already have been wrapped with PT_TO_ENUMERATION_VALUE expression or have the correct type
24655  */
24656  if ((list->node_type == PT_EXPR && list->info.expr.op == PT_TO_ENUMERATION_VALUE)
24657  || (list->type_enum == PT_TYPE_ENUMERATION
24658  && pt_is_same_enum_data_type (arg1->data_type, list->data_type)))
24659  {
24660  list_prev = list;
24661  list = list->next;
24662  continue;
24663  }
24664 
24665  save_next = list->next;
24666  list->next = NULL;
24667  node = pt_node_to_enumeration_expr (parser, arg1->data_type, list);
24668  if (node == NULL)
24669  {
24670  return NULL;
24671  }
24672 
24673  node->next = save_next;
24674  if (list_prev == NULL)
24675  {
24676  *list_start = node;
24677  }
24678  else
24679  {
24680  list_prev->next = node;
24681  }
24682  list_prev = node;
24683  list = node->next;
24684  }
24685  if (arg2->data_type != NULL)
24686  {
24687  parser_free_tree (parser, arg2->data_type);
24688  arg2->data_type = NULL;
24689  }
24690  (void) pt_add_type_to_set (parser, arg2->info.function.arg_list, &arg2->data_type);
24691  break;
24692 
24693  default:
24694  break;
24695  }
24696 
24697  expr->info.expr.arg1 = arg1;
24698  expr->info.expr.arg2 = arg2;
24699 
24700  return expr;
24701 }
24702 
24703 /*
24704  * pt_get_common_arg_type_of_width_bucket () -
24705  * get the common type of args that should have the same types
24706  * width_bucket (arg1, arg2, arg3, arg4);
24707  * arg2 and arg3 should be the same type
24708  * return:
24709  * parser(in):
24710  * node(in):
24711  */
24712 static PT_TYPE_ENUM
24713 pt_get_common_arg_type_of_width_bucket (PARSER_CONTEXT * parser, PT_NODE * node)
24714 {
24715  PT_TYPE_ENUM common_type = PT_TYPE_NONE;
24716  PT_NODE *arg1 = NULL;
24717  PT_NODE *arg2 = NULL;
24718  PT_NODE *arg3 = NULL;
24719  PT_NODE *between, *between_ge_lt;
24720 
24721  assert (node != NULL && node->node_type == PT_EXPR && node->info.expr.op == PT_WIDTH_BUCKET);
24722 
24723  arg1 = node->info.expr.arg1;
24724  assert (arg1 != NULL);
24725 
24726  between = node->info.expr.arg2;
24727  assert (between != NULL);
24728  if (between->node_type != PT_EXPR || between->info.expr.op != PT_BETWEEN)
24729  {
24730  return PT_TYPE_NONE;
24731  }
24732 
24733  between_ge_lt = between->info.expr.arg2;
24734  assert (between_ge_lt != NULL);
24735  if (between_ge_lt->node_type != PT_EXPR || between_ge_lt->info.expr.op != PT_BETWEEN_GE_LT)
24736  {
24737  return PT_TYPE_NONE;
24738  }
24739 
24740  arg2 = between_ge_lt->info.expr.arg1;
24741  arg3 = between_ge_lt->info.expr.arg2;
24742  if (arg2 == NULL || arg3 == NULL)
24743  {
24744  return PT_TYPE_NONE;
24745  }
24746 
24747  /* get the common type for arg2 and arg3 */
24748  common_type = pt_common_type (arg2->type_enum, arg3->type_enum);
24749  if (common_type == PT_TYPE_NONE)
24750  {
24751  /* if no common type for arg2 and arg3, try arg1_type */
24752  common_type = arg1->type_enum;
24753  }
24754 
24755  return common_type;
24756 }
24757 
24758 /*
24759  * pt_is_const_foldable_width_bucket () - check whether width_bucket function
24760  * is constant foldable or not.
24761  * return: true/false
24762  * parser(in):
24763  * expr(in):
24764  */
24765 static bool
24766 pt_is_const_foldable_width_bucket (PARSER_CONTEXT * parser, PT_NODE * expr)
24767 {
24768  PT_NODE *opd1 = NULL, *opd2 = NULL, *opd3 = NULL;
24769  PT_NODE *between_ge_lt = NULL;
24770  PT_NODE *between_ge_lt_arg1 = NULL, *between_ge_lt_arg2 = NULL;
24771 
24772  assert (expr->info.expr.op == PT_WIDTH_BUCKET);
24773 
24774  opd1 = expr->info.expr.arg1;
24775  opd2 = expr->info.expr.arg2;
24776  opd3 = expr->info.expr.arg3;
24777  assert (opd1 != NULL && opd2 != NULL && opd3 != NULL && opd2->node_type == PT_EXPR
24778  && opd2->info.expr.op == PT_BETWEEN);
24779 
24780  if (opd1->node_type == PT_VALUE && opd3->node_type == PT_VALUE)
24781  {
24782  between_ge_lt = opd2->info.expr.arg2;
24783  assert (between_ge_lt != NULL && between_ge_lt->node_type == PT_EXPR
24784  && between_ge_lt->info.expr.op == PT_BETWEEN_GE_LT);
24785 
24786  between_ge_lt_arg1 = between_ge_lt->info.expr.arg1;
24787  assert (between_ge_lt_arg1 != NULL);
24788 
24789  if (between_ge_lt_arg1->node_type == PT_VALUE)
24790  {
24791  between_ge_lt_arg2 = between_ge_lt->info.expr.arg2;
24792  assert (between_ge_lt_arg2 != NULL);
24793 
24794  if (between_ge_lt_arg2->node_type == PT_VALUE)
24795  {
24796  return true;
24797  }
24798  }
24799  }
24800 
24801  return false;
24802 }
24803 
24804 /*
24805  * pt_wrap_type_for_collation () - Determines the string type (VARCHAR or
24806  * VARNCHAR) to be used for wrap with cast or set as expected domain
24807  * onto a "TYPE_MAYBE" argument node. It also determines the
24808  * collection type to use if common argument type is collection.
24809  *
24810  * return: common type to use when wrapping with cast or setting expected
24811  * domain. If 'wrap_type_collection' is needed, this is the type
24812  * of collection component.
24813  *
24814  * arg1(in):
24815  * arg2(in):
24816  * arg3(in):
24817  * wrap_type_collection(out): collection type to use
24818  *
24819  * Note: this function assumes that mixed VARCHAR-VARNCHAR arguments have
24820  * already been detected as errors by type inference.
24821  */
24822 static PT_TYPE_ENUM
24823 pt_wrap_type_for_collation (const PT_NODE * arg1, const PT_NODE * arg2, const PT_NODE * arg3,
24824  PT_TYPE_ENUM * wrap_type_collection)
24825 {
24826  PT_TYPE_ENUM common_type = PT_TYPE_VARCHAR;
24827  PT_TYPE_ENUM arg1_type = PT_TYPE_NONE, arg2_type = PT_TYPE_NONE, arg3_type = PT_TYPE_NONE;
24828 
24829  if (arg1)
24830  {
24831  arg1_type = arg1->type_enum;
24832  }
24833 
24834  if (arg2)
24835  {
24836  arg2_type = arg2->type_enum;
24837  }
24838 
24839  if (arg3)
24840  {
24841  arg3_type = arg3->type_enum;
24842  }
24843 
24844  if (wrap_type_collection != NULL)
24845  {
24846  *wrap_type_collection = PT_TYPE_NONE;
24847  }
24848 
24850  || PT_IS_NATIONAL_CHAR_STRING_TYPE (arg3_type))
24851  {
24852  common_type = PT_TYPE_VARNCHAR;
24853  }
24854  else if (wrap_type_collection != NULL)
24855  {
24856  const PT_NODE *arg_collection = NULL;
24857  assert (!PT_IS_COLLECTION_TYPE (arg3_type));
24858 
24859  if (PT_IS_COLLECTION_TYPE (arg1_type))
24860  {
24861  *wrap_type_collection = arg1_type;
24862  arg_collection = arg1;
24863  }
24864  else if (PT_IS_COLLECTION_TYPE (arg2_type))
24865  {
24866  *wrap_type_collection = arg2_type;
24867  arg_collection = arg2;
24868  }
24869 
24870  if (arg_collection != NULL && arg_collection->data_type != NULL)
24871  {
24872  PT_NODE *dt;
24873  dt = arg_collection->data_type;
24874 
24875  /* check if wrap with cast is necessary */
24876  while (dt != NULL)
24877  {
24879  {
24880  common_type = dt->type_enum;
24881  break;
24882  }
24883  dt = dt->next;
24884  }
24885  }
24886  else if (arg_collection != NULL && arg_collection->expected_domain != NULL)
24887  {
24888  TP_DOMAIN *dom;
24889 
24890  dom = arg_collection->expected_domain->setdomain;
24891  while (dom != NULL)
24892  {
24893  if (TP_IS_CHAR_TYPE (TP_DOMAIN_TYPE (dom)))
24894  {
24895  common_type = pt_db_to_type_enum (TP_DOMAIN_TYPE (dom));
24896  break;
24897  }
24898  }
24899  }
24900  }
24901 
24902  return common_type;
24903 }
24904 
24905 /*
24906  * pt_fix_arguments_collation_flag () - checks an expression node having
24907  * arguments inner-expression of PT_CAST and clears their collation
24908  * flag if expression's signature allows it.
24909  *
24910  * return:
24911  * expr(in):
24912  *
24913  * Note : When doing walk tree, the collation inference may wrap with cast
24914  * the result of expression using TP_DOMAIN_COLL_ENFORCE flag (when the
24915  * collations of arguments can infer a common collation, but the type
24916  * cannot be strictly determined).
24917  * If such expression result is argument in an upper-level expression,
24918  * we atttempt promoting the cast domain to a normal one
24919  * (TP_DOMAIN_COLL_NORMAL).
24920  * Collation flag promoting is required for expressions which allow
24921  * only string type for a certain argument.
24922  * LOWER (? + col) -> the argument of LOWER has the same type as
24923  * 'col', and is CAST (? + col as type with TP_DOMAIN_COLL_ENFORCE)
24924  * This function, changes the CAST to a normal one.
24925  */
24926 static void
24927 pt_fix_arguments_collation_flag (PT_NODE * expr)
24928 {
24930  int i;
24931  PT_TYPE_ENUM arg1_sig_type = PT_TYPE_NONE, arg2_sig_type = PT_TYPE_NONE, arg3_sig_type = PT_TYPE_NONE;
24932  PT_NODE *arg;
24933 
24934  assert (expr->node_type == PT_EXPR);
24935 
24936  if ((expr->info.expr.arg1 == NULL || expr->info.expr.arg1->node_type != PT_EXPR
24937  || expr->info.expr.arg1->info.expr.op != PT_CAST)
24938  && (expr->info.expr.arg2 == NULL || expr->info.expr.arg2->node_type != PT_EXPR
24939  || expr->info.expr.arg2->info.expr.op != PT_CAST)
24940  && (expr->info.expr.arg3 == NULL || expr->info.expr.arg3->node_type != PT_EXPR
24941  || expr->info.expr.arg3->info.expr.op != PT_CAST))
24942  {
24943  return;
24944  }
24945 
24946  if (!pt_get_expression_definition (expr->info.expr.op, &def))
24947  {
24948  return;
24949  }
24950 
24951  /* for each argument, determine a common type between signatures : if all signatures allows only data types having
24952  * collation, we can promote the collation flag, if not - the signature type (argx_sig_type) is set to TYPE_NULL, and
24953  * the collation flag is not promoted */
24954  for (i = 0; i < def.overloads_count; i++)
24955  {
24956  PT_TYPE_ENUM arg_curr_sig_type;
24957 
24958  if (expr->info.expr.arg1 != NULL)
24959  {
24960  arg_curr_sig_type = pt_get_equivalent_type (def.overloads[i].arg1_type, expr->info.expr.arg1->type_enum);
24961 
24962  if (arg1_sig_type == PT_TYPE_NONE)
24963  {
24964  arg1_sig_type = arg_curr_sig_type;
24965  }
24966  else if (!PT_HAS_COLLATION (arg_curr_sig_type))
24967  {
24968  arg1_sig_type = PT_TYPE_NULL;
24969  }
24970  }
24971 
24972  if (expr->info.expr.arg2 != NULL)
24973  {
24974  arg_curr_sig_type = pt_get_equivalent_type (def.overloads[i].arg2_type, expr->info.expr.arg2->type_enum);
24975 
24976  if (arg2_sig_type == PT_TYPE_NONE)
24977  {
24978  arg2_sig_type = arg_curr_sig_type;
24979  }
24980  else if (!PT_HAS_COLLATION (arg_curr_sig_type))
24981  {
24982  arg2_sig_type = PT_TYPE_NULL;
24983  }
24984  }
24985 
24986  if (expr->info.expr.arg3 != NULL)
24987  {
24988  arg_curr_sig_type = pt_get_equivalent_type (def.overloads[i].arg3_type, expr->info.expr.arg3->type_enum);
24989 
24990 
24991  if (arg3_sig_type == PT_TYPE_NONE)
24992  {
24993  arg3_sig_type = arg_curr_sig_type;
24994  }
24995  else if (!PT_HAS_COLLATION (arg_curr_sig_type))
24996  {
24997  arg3_sig_type = PT_TYPE_NULL;
24998  }
24999  }
25000  }
25001 
25002  if (PT_HAS_COLLATION (arg1_sig_type) && expr->info.expr.arg1 != NULL)
25003  {
25004  arg = expr->info.expr.arg1;
25005 
25006  if (arg->node_type == PT_EXPR && arg->info.expr.op == PT_CAST && PT_HAS_COLLATION (arg->type_enum))
25007  {
25008  if (arg->data_type != NULL && arg->data_type->info.data_type.collation_flag == TP_DOMAIN_COLL_ENFORCE)
25009  {
25010  arg->data_type->info.data_type.collation_flag = TP_DOMAIN_COLL_NORMAL;
25011  }
25012 
25013  if (arg->info.expr.cast_type != NULL
25014  && arg->info.expr.cast_type->info.data_type.collation_flag == TP_DOMAIN_COLL_ENFORCE)
25015  {
25016  arg->info.expr.cast_type->info.data_type.collation_flag = TP_DOMAIN_COLL_NORMAL;
25017  }
25018  }
25019  }
25020 
25021  if (PT_HAS_COLLATION (arg2_sig_type) && expr->info.expr.arg2 != NULL)
25022  {
25023  arg = expr->info.expr.arg2;
25024 
25025  if (arg->node_type == PT_EXPR && arg->info.expr.op == PT_CAST && PT_HAS_COLLATION (arg->type_enum))
25026  {
25027  if (arg->data_type != NULL && arg->data_type->info.data_type.collation_flag == TP_DOMAIN_COLL_ENFORCE)
25028  {
25029  arg->data_type->info.data_type.collation_flag = TP_DOMAIN_COLL_NORMAL;
25030  }
25031 
25032  if (arg->info.expr.cast_type != NULL
25033  && arg->info.expr.cast_type->info.data_type.collation_flag == TP_DOMAIN_COLL_ENFORCE)
25034  {
25035  arg->info.expr.cast_type->info.data_type.collation_flag = TP_DOMAIN_COLL_NORMAL;
25036  }
25037  }
25038  }
25039 
25040  if (PT_HAS_COLLATION (arg3_sig_type) && expr->info.expr.arg3 != NULL)
25041  {
25042  arg = expr->info.expr.arg3;
25043 
25044  if (arg->node_type == PT_EXPR && arg->info.expr.op == PT_CAST && PT_HAS_COLLATION (arg->type_enum))
25045  {
25046  if (arg->data_type != NULL && arg->data_type->info.data_type.collation_flag == TP_DOMAIN_COLL_ENFORCE)
25047  {
25048  arg->data_type->info.data_type.collation_flag = TP_DOMAIN_COLL_NORMAL;
25049  }
25050 
25051  if (arg->info.expr.cast_type != NULL
25052  && arg->info.expr.cast_type->info.data_type.collation_flag == TP_DOMAIN_COLL_ENFORCE)
25053  {
25054  arg->info.expr.cast_type->info.data_type.collation_flag = TP_DOMAIN_COLL_NORMAL;
25055  }
25056  }
25057  }
25058 }
25059 
25060 /*
25061  * pt_check_function_collation () - checks the collation of function
25062  * (PT_FUNCTION)
25063  *
25064  * return: error code
25065  * parser(in): parser context
25066  * node(in/out): a parse tree function node
25067  *
25068  */
25069 static PT_NODE *
25070 pt_check_function_collation (PARSER_CONTEXT * parser, PT_NODE * node)
25071 {
25072  PT_NODE *arg_list, *arg, *prev_arg, *new_node;
25073  PT_COLL_INFER common_coll_infer, res_coll_infer;
25074  bool need_arg_coerc = false;
25075  TP_DOMAIN_COLL_ACTION res_collation_flag = TP_DOMAIN_COLL_LEAVE;
25076  FUNC_TYPE fcode;
25077 
25078  assert (node != NULL);
25079 
25080  assert (node != NULL);
25081  assert (node->node_type == PT_FUNCTION);
25082 
25083  if (node->info.function.arg_list == NULL)
25084  {
25085  return node;
25086  }
25087 
25088  fcode = node->info.function.function_type;
25089  arg_list = node->info.function.arg_list;
25090  prev_arg = NULL;
25091 
25092  if (fcode == F_ELT)
25093  {
25094  if (arg_list->next == NULL)
25095  {
25096  return node;
25097  }
25098  arg_list = arg_list->next;
25099  prev_arg = arg_list;
25100  }
25101 
25102  arg = arg_list;
25103 
25104  common_coll_infer.coll_id = -1;
25105  common_coll_infer.codeset = INTL_CODESET_NONE;
25106  common_coll_infer.can_force_cs = true;
25107  common_coll_infer.coerc_level = PT_COLLATION_NOT_APPLICABLE;
25108  while (arg != NULL)
25109  {
25110  PT_COLL_INFER arg_coll_infer;
25111 
25112  arg_coll_infer.coll_id = -1;
25113 
25114  if (arg->type_enum != PT_TYPE_MAYBE && !(PT_IS_CAST_MAYBE (arg)))
25115  {
25116  res_collation_flag = TP_DOMAIN_COLL_NORMAL;
25117  }
25118 
25119  if ((PT_HAS_COLLATION (arg->type_enum) || arg->type_enum == PT_TYPE_MAYBE)
25120  && pt_get_collation_info (arg, &arg_coll_infer))
25121  {
25122  bool apply_common_coll = false;
25123  int common_coll;
25124  INTL_CODESET common_cs;
25125 
25126  if (common_coll_infer.coll_id != -1 || common_coll_infer.coll_id != arg_coll_infer.coll_id)
25127  {
25128  need_arg_coerc = true;
25129  }
25130 
25131  if (common_coll_infer.coll_id == -1)
25132  {
25133  apply_common_coll = true;
25134  }
25135  else
25136  {
25137  if (pt_common_collation (&common_coll_infer, &arg_coll_infer, NULL, 2, false, &common_coll, &common_cs) ==
25138  0)
25139  {
25140  if (common_coll != common_coll_infer.coll_id)
25141  {
25142  apply_common_coll = true;
25143  }
25144  }
25145  else
25146  {
25147  goto error_collation;
25148  }
25149  }
25150 
25151  if (apply_common_coll)
25152  {
25153  common_coll_infer = arg_coll_infer;
25154  }
25155  }
25156 
25157  arg = arg->next;
25158  }
25159 
25160  if (common_coll_infer.coll_id == -1)
25161  {
25162  return node;
25163  }
25164 
25165  arg = arg_list;
25166  while (need_arg_coerc && arg != NULL)
25167  {
25168  PT_COLL_INFER arg_coll_infer;
25169 
25170  arg_coll_infer.coll_id = -1;
25171 
25172  if (!(PT_HAS_COLLATION (arg->type_enum) || arg->type_enum == PT_TYPE_MAYBE))
25173  {
25174  prev_arg = arg;
25175  arg = arg->next;
25176  continue;
25177  }
25178 
25179  if (!pt_get_collation_info (arg, &arg_coll_infer))
25180  {
25181  prev_arg = arg;
25182  arg = arg->next;
25183  continue;
25184  }
25185 
25186  if (common_coll_infer.coll_id != arg_coll_infer.coll_id)
25187  {
25188  PT_NODE *save_next;
25189 
25190  save_next = arg->next;
25191 
25192  new_node =
25193  pt_coerce_node_collation (parser, arg, common_coll_infer.coll_id, common_coll_infer.codeset,
25194  arg_coll_infer.can_force_cs, false, PT_COLL_WRAP_TYPE_FOR_MAYBE (arg->type_enum),
25195  PT_TYPE_NONE);
25196 
25197  if (new_node != NULL)
25198  {
25199  arg = new_node;
25200  if (prev_arg == NULL)
25201  {
25202  node->info.function.arg_list = arg;
25203  }
25204  else
25205  {
25206  prev_arg->next = arg;
25207  }
25208  arg->next = save_next;
25209  }
25210  }
25211 
25212  prev_arg = arg;
25213  arg = arg->next;
25214  }
25215 
25216  if (need_arg_coerc)
25217  {
25218  switch (fcode)
25219  {
25220  case F_SET:
25221  case F_MULTISET:
25222  case F_SEQUENCE:
25223  /* add the new data_type to the set of data_types */
25224  pt_add_type_to_set (parser, arg_list, &node->data_type);
25225  break;
25226  default:
25227  break;
25228  }
25229  }
25230 
25231  if (PT_HAS_COLLATION (node->type_enum) && res_collation_flag == TP_DOMAIN_COLL_LEAVE && node->data_type != NULL)
25232  {
25233  node->data_type->info.data_type.collation_flag = res_collation_flag;
25234  }
25235  else if ((PT_HAS_COLLATION (node->type_enum) || node->type_enum == PT_TYPE_MAYBE)
25236  && pt_get_collation_info (node, &res_coll_infer) && res_coll_infer.coll_id != common_coll_infer.coll_id)
25237  {
25238  new_node =
25239  pt_coerce_node_collation (parser, node, common_coll_infer.coll_id, common_coll_infer.codeset, true, false,
25241  if (new_node != NULL)
25242  {
25243  node = new_node;
25244  }
25245  }
25246 
25247  return node;
25248 
25249 error_collation:
25251  pt_show_function (node->info.function.function_type));
25252  return node;
25253 }
25254 
25255 /* pt_hv_consistent_data_type_with_domain - update data type of HOST_VAR node
25256  * to be the same with expected domain
25257  * If data_type is not present, no
25258  * change is made.
25259  * return: void
25260  * node (in/out) :
25261  */
25262 static void
25263 pt_hv_consistent_data_type_with_domain (PARSER_CONTEXT * parser, PT_NODE * node)
25264 {
25265  PT_NODE *p = NULL;
25266 
25267  assert (node != NULL);
25268  assert (node->expected_domain != NULL);
25269 
25270  if (node->node_type != PT_HOST_VAR)
25271  {
25272  return;
25273  }
25274 
25275  pt_update_host_var_data_type (parser, node);
25276 
25277  for (p = node->or_next; p != NULL; p = p->or_next)
25278  {
25279  pt_update_host_var_data_type (parser, p);
25280  }
25281 }
25282 
25283 /* pt_update_host_var_data_type - update data type of HOST_VAR node
25284  * to be the same with expected domain
25285  * If data_type is not present, no
25286  * change is made.
25287  * return: void
25288  * hv_node (in/out) :
25289  */
25290 static void
25291 pt_update_host_var_data_type (PARSER_CONTEXT * parser, PT_NODE * hv_node)
25292 {
25293  PT_NODE *dt;
25294  TP_DOMAIN *dom;
25295 
25296  if (hv_node->node_type != PT_HOST_VAR || hv_node->data_type == NULL || hv_node->expected_domain == NULL)
25297  {
25298  return;
25299  }
25300 
25301  dt = hv_node->data_type;
25302  dom = hv_node->expected_domain;
25303 
25305  {
25307  dt->info.data_type.units = dom->codeset;
25309  }
25310  else if (dt->type_enum != pt_db_to_type_enum (TP_DOMAIN_TYPE (dom))
25311  || (dt->type_enum == PT_TYPE_NUMERIC
25312  && (dt->info.data_type.precision != dom->precision || dt->info.data_type.dec_precision != dom->scale)))
25313  {
25314  parser_free_node (parser, hv_node->data_type);
25315  hv_node->data_type = pt_domain_to_data_type (parser, dom);
25316  }
25317 }
25318 
25319 static bool
25320 pt_cast_needs_wrap_for_collation (PT_NODE * node, const INTL_CODESET codeset)
25321 {
25322  assert (node != NULL);
25323  assert (node->node_type == PT_EXPR);
25324  assert (node->info.expr.op == PT_CAST);
25325 
25326  if (node->info.expr.arg1 != NULL && node->info.expr.arg1->data_type != NULL && node->info.expr.cast_type != NULL
25327  && PT_HAS_COLLATION (node->info.expr.arg1->type_enum) && PT_HAS_COLLATION (node->info.expr.cast_type->type_enum)
25328  && node->info.expr.arg1->data_type->info.data_type.units != node->info.expr.cast_type->info.data_type.units
25329  && node->info.expr.cast_type->info.data_type.units != codeset)
25330  {
25331  return true;
25332  }
25333 
25334  return false;
25335 }
#define ER_IT_INCOMPATIBLE_DATATYPE
Definition: error_code.h:499
int julian_encode(int m, int d, int y)
Definition: db_date.c:113
DB_OBJECT * db_find_class(const char *name)
Definition: db_info.c:134
#define PT_NAME_INFO_SET_FLAG(e, f)
Definition: parse_tree.h:2590
#define ER_REGEX_COMPILE_ERROR
Definition: error_code.h:1358
int db_inet_ntoa(DB_VALUE *result_ip_string, const DB_VALUE *number)
PT_NODE * pt_name(PARSER_CONTEXT *parser_ptr, const char *name)
DB_C_FLOAT db_get_float(const DB_VALUE *value)
int db_sin_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:3769
PT_NODE * next
Definition: parse_tree.h:3435
PT_NAME_INFO name
Definition: parse_tree.h:3305
int db_date_format(const DB_VALUE *date_value, const DB_VALUE *format, const DB_VALUE *date_lang, DB_VALUE *result, const TP_DOMAIN *domain)
int db_set_compare(const DB_VALUE *value1, const DB_VALUE *value2)
Definition: db_macro.c:1828
int set_difference(DB_COLLECTION *collection1, DB_COLLECTION *collection2, DB_COLLECTION **result, DB_DOMAIN *domain)
Definition: set_object.c:3896
#define TP_IS_DATE_OR_TIME_TYPE(typeid)
TP_DOMAIN_STATUS tp_value_coerce(const DB_VALUE *src, DB_VALUE *dest, const TP_DOMAIN *desired_domain)
int db_time_format(const DB_VALUE *src_value, const DB_VALUE *format, const DB_VALUE *date_lang, DB_VALUE *result, const TP_DOMAIN *domain)
#define PT_HAS_TIME_PART(t)
Definition: parse_tree.h:213
PT_OP_TYPE pt_negate_op(PT_OP_TYPE op)
PT_NODE * pt_wrap_with_cast_op(PARSER_CONTEXT *parser, PT_NODE *arg, PT_TYPE_ENUM new_type, int p, int s, PT_NODE *desired_dt)
unsigned is_wrapped_res_for_coll
Definition: parse_tree.h:3468
int db_make_datetime(DB_VALUE *value, const DB_DATETIME *datetime)
#define PT_IS_QUERY(n)
Definition: parse_tree.h:295
int db_find_string_in_in_set(const DB_VALUE *needle, const DB_VALUE *stack, DB_VALUE *result)
PT_NODE * arg_list
Definition: parse_tree.h:2272
#define NO_ERROR
Definition: error_code.h:42
PT_UNION_INFO union_
Definition: parse_tree.h:2794
int pt_has_error(const PARSER_CONTEXT *parser)
MISC_OPERAND pt_misc_to_qp_misc_operand(PT_MISC_TYPE misc_specifier)
Definition: parse_dbi.c:91
int db_get_date_weekday(const DB_VALUE *src_date, const int mode, DB_VALUE *result)
int db_get_date_quarter(const DB_VALUE *src_date, DB_VALUE *result)
int db_get_date_totaldays(const DB_VALUE *src_date, DB_VALUE *result)
#define ER_DB_UNIMPLEMENTED
Definition: error_code.h:195
#define PT_EXPR_INFO_CAST_SHOULD_FOLD
Definition: parse_tree.h:2242
#define DB_ROW_COUNT_NOT_SET
Definition: dbtype_def.h:513
DB_COLLECTION * db_get_set(const DB_VALUE *value)
#define DB_MAX_BIT_PRECISION
Definition: dbtype_def.h:574
#define LANG_SYS_COLLATION
int pr_data_writeval_disk_size(DB_VALUE *value)
bool pt_is_aggregate_function(PARSER_CONTEXT *parser, const PT_NODE *node)
#define LANG_GET_BINARY_COLLATION(c)
int db_string_space(DB_VALUE const *count, DB_VALUE *result)
PT_ARG_TYPE return_type
UINTPTR id
Definition: parse_tree.h:2159
#define PT_COLL_WRAP_TYPE_FOR_MAYBE(type)
PT_OP_TYPE pt_converse_op(PT_OP_TYPE op)
TP_DOMAIN * tp_domain_resolve_default_w_coll(DB_TYPE type, int coll_id, TP_DOMAIN_COLL_ACTION coll_flag)
int tz_timestamptz_fix_zone(const DB_TIMESTAMPTZ *src_ts_tz, DB_TIMESTAMPTZ *dest_ts_tz)
Definition: tz_support.c:1834
int pt_comp_to_between_op(PT_OP_TYPE left, PT_OP_TYPE right, PT_COMP_TO_BETWEEN_OP_CODE_TYPE type, PT_OP_TYPE *between)
DB_VALUE_COMPARE_RESULT tp_value_compare(const DB_VALUE *value1, const DB_VALUE *value2, int allow_coercion, int total_order)
#define PT_IS_CHAR_STRING_TYPE(t)
Definition: parse_tree.h:163
PT_STATEMENT_INFO info
Definition: parse_tree.h:3471
TP_DOMAIN * expected_domain
Definition: parse_tree.h:3440
pt_generic_type_enum
#define PT_ERRORm(parser, node, setNo, msgNo)
Definition: parse_tree.h:62
#define ASSERT_ERROR()
PT_NODE * using_index
Definition: parse_tree.h:2708
#define MSGCAT_SEMANTIC_CANT_EXTRACT_FROM
#define MSGCAT_SEMANTIC_COLLATE_NOT_ALLOWED
#define db_locate_numeric(value)
STATEMENT_SET_FOLD
PT_NODE * pt_where_type_keep_true(PARSER_CONTEXT *parser, PT_NODE *where)
#define MSGCAT_SEMANTIC_FUNC_NOT_DEFINED_ON_2
int
Definition: mprec.c:252
int pt_type_generic_func(PARSER_CONTEXT *parser, PT_NODE *node)
TP_DOMAIN * tp_domain_select(const TP_DOMAIN *domain_list, const DB_VALUE *value, int allow_coercion, TP_MATCH exact_match)
#define MSGCAT_SEMANTIC_WANT_LOGICAL_WHERE
bool pt_is_op_hv_late_bind(PT_OP_TYPE op)
#define QSTR_IS_ANY_CHAR(s)
Definition: string_opfunc.h:47
unsigned char codeset
Definition: object_domain.h:93
struct pt_arg_type PT_ARG_TYPE
STATEMENT_SET_FOLD pt_check_union_is_foldable(PARSER_CONTEXT *parser, PT_NODE *union_node)
int set_issome(DB_VALUE *value, DB_COLLECTION *set, PT_OP_TYPE op, int do_coercion)
Definition: set_object.c:3231
#define MSGCAT_SEMANTIC_DATA_OVERFLOW_ON
bool obt_Last_insert_id_generated
int db_bigint_to_binary_string(const DB_VALUE *src_bigint, DB_VALUE *result)
#define TP_IS_SET_TYPE(typenum)
int db_make_string(DB_VALUE *value, char *str)
bool is_collate_allowed
Definition: parse_tree.h:3081
int db_add_int_to_datetime(DB_DATETIME *datetime, DB_BIGINT bi2, DB_DATETIME *result_datetime)
Definition: db_date.c:4636
PT_MISC_TYPE
Definition: parse_tree.h:983
int db_string_chr(DB_VALUE *res, DB_VALUE *dbval1, DB_VALUE *dbval2)
collation_result
int db_string_index_prefix(const DB_VALUE *string1, const DB_VALUE *string2, const DB_VALUE *index_type, DB_VALUE *prefix_index)
int db_make_bigint(DB_VALUE *value, const DB_BIGINT num)
int db_get_int(const DB_VALUE *value)
Definition: queue.h:29
DB_TIMESTAMP timestamp
Definition: dbtype_def.h:791
int db_json_merge(DB_VALUE *result, DB_VALUE *arg[], int const num_args)
int db_json_array(DB_VALUE *result, DB_VALUE *arg[], int const num_args)
int tp_more_general_type(const DB_TYPE type1, const DB_TYPE type2)
#define DB_DEFAULT_NUMERIC_DIVISION_SCALE
Definition: dbtype_def.h:595
int db_string_insert_substring(DB_VALUE *src_string, const DB_VALUE *position, const DB_VALUE *length, DB_VALUE *sub_string, DB_VALUE *result)
DB_TYPE
Definition: dbtype_def.h:695
#define LANG_IS_COERCIBLE_COLL(c)
DB_C_DOUBLE db_get_double(const DB_VALUE *value)
void tz_timestamp_decode_no_leap_sec(int timestamp, int *yearp, int *monthsp, int *dayp, int *hoursp, int *minutesp, int *secondsp)
Definition: tz_support.c:601
#define ER_FAILED
Definition: error_code.h:43
void numeric_coerce_num_to_double(DB_C_NUMERIC num, int scale, double *adouble)
int db_char_to_blob(const DB_VALUE *src_value, DB_VALUE *result_value)
#define COMPARE_BETWEEN_OPERATOR_COUNT
int db_json_remove(DB_VALUE *result, DB_VALUE *arg[], int const num_args)
PT_TYPE_ENUM recursive_type
Definition: parse_tree.h:2258
int db_new_time(DB_VALUE *time_val, DB_VALUE *tz_source, DB_VALUE *tz_dest, DB_VALUE *result_time)
int pt_node_list_to_array(PARSER_CONTEXT *parser, PT_NODE *arg_list, PT_NODE *arg_array[], const int array_size, int *num_args)
int db_convert_sec_to_time(const DB_VALUE *src, DB_VALUE *result)
#define MSGCAT_SEMANTIC_OUT_OF_MEMORY
PT_GENERIC_TYPE_ENUM generic_type
int db_get_string_collation(const DB_VALUE *value)
#define PT_EXPR_INFO_SET_FLAG(e, f)
Definition: parse_tree.h:2252
d d
Definition: mprec.c:783
int numeric_db_value_div(const DB_VALUE *dbv1, const DB_VALUE *dbv2, DB_VALUE *answer)
int numeric_db_value_negate(DB_VALUE *answer)
#define MSGCAT_SEMANTIC_COLLATION_OP_ERROR
struct tp_domain * setdomain
Definition: object_domain.h:84
PT_NODE * arg3
Definition: parse_tree.h:2217
PT_SPEC_INFO spec
Definition: parse_tree.h:3333
#define PT_NODE_DATA_TYPE(n)
Definition: parse_tree.h:473
int db_timestamp_encode_ses(const DB_DATE *date, const DB_TIME *timeval, DB_TIMESTAMP *utime, TZ_ID *dest_tz_id)
Definition: db_date.c:597
PT_NODE * pt_get_select_list(PARSER_CONTEXT *parser, PT_NODE *query)
Definition: query_result.c:403
PT_NODE * arg2
Definition: parse_tree.h:2679
PT_NODE * pt_make_prim_data_type_fortonum(PARSER_CONTEXT *parser, int prec, int scale)
int db_value_clone(DB_VALUE *src, DB_VALUE *dest)
Definition: db_macro.c:1558
#define MSGCAT_SEMANTIC_OP_NOT_DEFINED_ON_1
#define PT_EXPR_INFO_CAST_NOFAIL
Definition: parse_tree.h:2240
void set_make_collection(DB_VALUE *value, DB_COLLECTION *col)
Definition: set_object.c:3956
PT_TYPE_ENUM pt_common_type(PT_TYPE_ENUM arg1_type, PT_TYPE_ENUM arg2_type)
#define PT_NODE_PRINT_TO_ALIAS(p, n, c)
Definition: parse_tree.h:582
union pt_query_info::@174 q
short continued_case
Definition: parse_tree.h:2255
#define SECONDS_OF_ONE_DAY
enum pt_type_enum PT_TYPE_ENUM
Definition: parse_tree.h:962
int db_make_numeric(DB_VALUE *value, const DB_C_NUMERIC num, const int precision, const int scale)
struct setobj * set
unsigned is_added_by_parser
Definition: parse_tree.h:3466
bool numeric_db_value_is_zero(const DB_VALUE *arg)
int db_power_dbval(DB_VALUE *result, DB_VALUE *value1, DB_VALUE *value2)
Definition: arithmetic.c:828
int db_asin_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:3921
void tz_get_session_tz_region(TZ_REGION *tz_region)
Definition: tz_support.c:767
#define MAX_OVERLOADS
#define pt_is_query(n)
Definition: parse_tree.h:258
#define DB_MAX_NCHAR_PRECISION
Definition: dbtype_def.h:566
#define ER_QSTR_INVALID_DATA_TYPE
Definition: error_code.h:742
int db_subtract_int_from_datetime(DB_DATETIME *dt1, DB_BIGINT bi2, DB_DATETIME *result_datetime)
Definition: db_date.c:4592
DB_C_NUMERIC db_get_numeric(const DB_VALUE *value)
void pt_set_expected_domain(PT_NODE *node, TP_DOMAIN *domain)
DB_DOMAIN * pt_node_data_type_to_db_domain(PARSER_CONTEXT *parser, PT_NODE *dt, PT_TYPE_ENUM type)
Definition: parse_dbi.c:2029
PT_EXPR_INFO expr
Definition: parse_tree.h:3289
FUNC_TYPE
#define OR_CHECK_SHORT_DIV_OVERFLOW(a, b)
int setobj_get_element_ptr(COL *col, int index, DB_VALUE **result)
Definition: set_object.c:5699
int db_json_set(DB_VALUE *result, DB_VALUE *arg[], int const num_args)
#define DB_DATE_MIN
Definition: dbtype_def.h:674
int db_value_compare(const DB_VALUE *value1, const DB_VALUE *value2)
Definition: db_macro.c:1850
#define MSGCAT_SEMANTIC_CS_MATCH_COLLATE
int set_size(DB_COLLECTION *set)
Definition: set_object.c:3027
PT_TYPE_ENUM pt_db_to_type_enum(const DB_TYPE t)
Definition: parse_dbi.c:2595
char * DB_C_NCHAR
Definition: dbtype_def.h:1180
#define pt_is_input_parameter(n)
Definition: parse_tree.h:272
INTL_LANG lang_get_lang_id_from_flag(const int flag, bool *has_user_format, bool *has_user_lang)
LPOLESTR DBORDINAL flags BYTE BYTE scale
int db_string_reverse(const DB_VALUE *src_str, DB_VALUE *result_str)
#define PT_IS_LOB_TYPE(t)
Definition: parse_tree.h:239
bool pt_is_json_doc_type(PT_TYPE_ENUM type)
Definition: parse_tree.c:1425
#define MSGCAT_SEMANTIC_COLLECTION_EL_COLLATION_ERROR
#define PT_ERRORmf5(parser, node, setNo, msgNo, arg1, arg2, arg3, arg4, arg5)
Definition: parse_tree.h:67
int db_json_get_all_paths(DB_VALUE *result, DB_VALUE *arg[], int const num_args)
#define PT_IS_DATE_TIME_TYPE(t)
Definition: parse_tree.h:194
const char * pt_show_function(FUNC_TYPE c)
int db_make_date(DB_VALUE *value, const int month, const int day, const int year)
bool pt_is_json_value_type(PT_TYPE_ENUM type)
Definition: parse_tree.c:1412
#define OID_SET_NULL(oidp)
Definition: oid.h:86
DB_DATETIMETZ * db_get_datetimetz(const DB_VALUE *value)
int db_make_datetimeltz(DB_VALUE *value, const DB_DATETIME *datetime)
#define OR_CHECK_ADD_OVERFLOW(a, b, c)
int db_exp_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:651
int db_round_dbval(DB_VALUE *result, DB_VALUE *value1, DB_VALUE *value2)
Definition: arithmetic.c:2314
int db_string_from_base64(DB_VALUE const *src, DB_VALUE *result)
#define TP_IS_STRING_TYPE(typeid)
#define PT_NODE_IS_SESSION_VARIABLE(node)
TP_DOMAIN * tp_domain_copy(const TP_DOMAIN *domain, bool check_cache)
#define PT_IS_NULL_NODE(e)
Definition: parse_tree.h:121
PT_NODE * group_by
Definition: parse_tree.h:2703
DB_DOMAIN * pt_node_to_db_domain(PARSER_CONTEXT *parser, PT_NODE *node, const char *class_name)
Definition: parse_dbi.c:2244
int tz_conv_tz_time_w_zone_name(const DB_TIME *time_source, const char *source_zone, int len_source, const char *dest_zone, int len_dest, DB_TIME *time_dest)
Definition: tz_support.c:1519
TP_DOMAIN tp_Integer_domain
int er_errid(void)
int db_time_dbval(DB_VALUE *result, const DB_VALUE *datetime_value, const TP_DOMAIN *domain)
#define PT_IS_STRING_TYPE(t)
Definition: parse_tree.h:147
int db_from_unixtime(const DB_VALUE *src_value, const DB_VALUE *format, const DB_VALUE *date_lang, DB_VALUE *result, const TP_DOMAIN *domain)
#define PT_NAME_INFO_IS_FLAGED(e, f)
Definition: parse_tree.h:2589
void db_string_free(char *string)
Definition: db_admin.c:2586
bool pt_false_where(PARSER_CONTEXT *parser, PT_NODE *node)
PT_NODE_LIST_INFO node_list
Definition: parse_tree.h:3307
int db_string_sha_one(DB_VALUE const *src, DB_VALUE *result)
int db_to_timestamp(const DB_VALUE *src_str, const DB_VALUE *format_str, const DB_VALUE *date_lang, const DB_TYPE type, DB_VALUE *result_timestamp)
int numeric_db_value_sub(const DB_VALUE *dbv1, const DB_VALUE *dbv2, DB_VALUE *answer)
#define TP_TIMESTAMPTZ_AS_CHAR_LENGTH
int * offset
Definition: expat.h:891
#define DB_VALUE_PRECISION(value)
Definition: dbtype.h:71
int tz_conv_tz_datetime_w_region(const DB_DATETIME *src_dt, const TZ_REGION *src_tz_region, const TZ_REGION *dest_tz_region, DB_DATETIME *dest_dt, TZ_ID *src_tz_id_out, TZ_ID *dest_tz_id_out)
Definition: tz_support.c:3801
int pt_between_to_comp_op(PT_OP_TYPE between, PT_OP_TYPE *left, PT_OP_TYPE *right)
#define MSGCAT_SEMANTIC_FUNC_NOT_DEFINED_ON_4
int db_add_time(const DB_VALUE *left, const DB_VALUE *right, DB_VALUE *result, const TP_DOMAIN *domain)
enum tp_domain_status TP_DOMAIN_STATUS
const char * pt_show_misc_type(PT_MISC_TYPE p)
int db_timestamp_encode_sys(const DB_DATE *date, const DB_TIME *timeval, DB_TIMESTAMP *utime, TZ_ID *dest_tz_id)
Definition: db_date.c:617
int db_sign_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:421
PT_NODE * pt_where_type(PARSER_CONTEXT *parser, PT_NODE *where)
void pt_add_type_to_set(PARSER_CONTEXT *parser, const PT_NODE *typs, PT_NODE **set)
Definition: parse_dbi.c:198
int db_json_replace(DB_VALUE *result, DB_VALUE *arg[], int const num_args)
#define MSGCAT_SEMANTIC_TIME_UNDERFLOW
int db_blob_length(const DB_VALUE *src_value, DB_VALUE *result_value)
void pt_to_regu_resolve_domain(int *p_precision, int *p_scale, const PT_NODE *node)
PT_NODE * data_type
Definition: parse_tree.h:3441
#define MSGCAT_SEMANTIC_IS_NOT_A
#define DB_DEFAULT_NUMERIC_SCALE
Definition: dbtype_def.h:592
name
Definition: setup.py:25
INTL_CODESET codeset
Definition: parse_tree.h:3650
int db_atan2_dbval(DB_VALUE *result, DB_VALUE *value, DB_VALUE *value2)
Definition: arithmetic.c:4006
const char * s
Definition: expat.h:713
int db_log_generic_dbval(DB_VALUE *result, DB_VALUE *value, long b)
Definition: arithmetic.c:4118
#define MSGCAT_SEMANTIC_WANT_LOGICAL_CASE_COND
int db_string_substring(const MISC_OPERAND substr_operand, const DB_VALUE *src_string, const DB_VALUE *start_position, const DB_VALUE *extraction_length, DB_VALUE *sub_string)
PT_DOT_INFO dot
Definition: parse_tree.h:3277
DB_DOMAIN_INFO domain
Definition: dbtype_def.h:1107
PT_NODE * arg2
Definition: parse_tree.h:2101
#define DB_VALUE_SCALE(value)
Definition: dbtype.h:72
int db_sleep(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:4976
#define MSGCAT_SEMANTIC_WANT_TYPE
#define QSTR_IS_BIT(s)
Definition: string_opfunc.h:45
TP_DOMAIN * pt_xasl_node_to_domain(PARSER_CONTEXT *parser, const PT_NODE *node)
bool pt_is_operator_logical(PT_OP_TYPE op)
#define TP_DOUBLE_AS_CHAR_LENGTH
#define MSGCAT_SEMANTIC_CANT_COERCE_TO
void pt_preset_hostvar(PARSER_CONTEXT *parser, PT_NODE *hv_node)
#define OR_CHECK_BIGINT_DIV_OVERFLOW(a, b)
PT_CTE_INFO cte
Definition: parse_tree.h:3272
DB_TIMESTAMPTZ * db_get_timestamptz(const DB_VALUE *value)
int db_make_short(DB_VALUE *value, const DB_C_SHORT num)
FUNC_TYPE function_type
Definition: parse_tree.h:2273
PT_TYPE_ENUM type_enum
Definition: parse_tree.h:3444
PT_NODE * or_next
Definition: parse_tree.h:3436
int db_check_time_date_format(const char *format_s)
Definition: db_date.c:4894
int db_string_aes_decrypt(DB_VALUE const *src, DB_VALUE const *key, DB_VALUE *result)
DB_TYPE pt_type_enum_to_db(const PT_TYPE_ENUM t)
Definition: parse_dbi.c:2314
return b
Definition: mprec.c:210
bool pt_list_has_logical_nodes(PT_NODE *list)
DB_MONETARY * db_get_monetary(const DB_VALUE *value)
#define PT_ERRORmf3(parser, node, setNo, msgNo, arg1, arg2, arg3)
Definition: parse_tree.h:65
int db_json_contains_dbval(const DB_VALUE *json, const DB_VALUE *value, const DB_VALUE *path, DB_VALUE *result)
Definition: arithmetic.c:5074
int db_string_position(const DB_VALUE *sub_string, const DB_VALUE *src_string, DB_VALUE *result)
PT_MISC_TYPE derived_table_type
Definition: parse_tree.h:2162
unsigned int DB_TIMESTAMP
Definition: dbtype_def.h:784
int db_unix_timestamp(const DB_VALUE *src_date, DB_VALUE *result_timestamp)
#define OR_CHECK_DOUBLE_OVERFLOW(i)
#define DB_MAX_VARBIT_PRECISION
Definition: dbtype_def.h:577
PT_NODE * cast_type
Definition: parse_tree.h:2218
PT_NODE * arg1
Definition: parse_tree.h:2678
int pt_wrap_select_list_with_cast_op(PARSER_CONTEXT *parser, PT_NODE *query, PT_TYPE_ENUM new_type, int p, int s, PT_NODE *data_type, bool force_wrap)
int set_union(DB_COLLECTION *collection1, DB_COLLECTION *collection2, DB_COLLECTION **result, DB_DOMAIN *domain)
Definition: set_object.c:3919
enum pt_coll_coerc_lev PT_COLL_COERC_LEV
Definition: parse_tree.h:3644
#define PT_NODE_PRINT_VALUE_TO_TEXT(p, n)
Definition: parse_tree.h:601
int db_add_days_to_year(const DB_VALUE *src_year, const DB_VALUE *src_days, DB_VALUE *result)
int pt_is_between_range_op(PT_OP_TYPE op)
TP_DOMAIN * tp_domain_resolve_default(DB_TYPE type)
int db_string_concatenate(const DB_VALUE *string1, const DB_VALUE *string2, DB_VALUE *result, DB_DATA_STATUS *data_status)
void er_set(int severity, const char *file_name, const int line_no, int err_id, int num_args,...)
bool db_string_check_explicit_date(const char *str, int str_len)
Definition: db_date.c:3609
INTL_CODESET codeset
int db_string_lower(const DB_VALUE *string, DB_VALUE *lower_string)
PT_NODE * arg2
Definition: parse_tree.h:2213
LANG_COLLATION * lang_get_collation(const int coll_id)
#define TP_TIMESTAMP_AS_CHAR_LENGTH
PT_FUNCTION_INFO function
Definition: parse_tree.h:3291
int db_cot_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:3833
#define TP_FLOAT_AS_CHAR_LENGTH
int db_value_put_encoded_time(DB_VALUE *value, const DB_TIME *time)
Definition: db_macro.c:1351
#define CAST_POINTER_TO_NODE(p)
Definition: parse_tree.h:611
#define DB_MAX_VARCHAR_PRECISION
Definition: dbtype_def.h:561
DB_OBJECT * db_object
Definition: parse_tree.h:2561
#define DB_MAX_NUMERIC_PRECISION
Definition: dbtype_def.h:549
int db_make_nchar(DB_VALUE *value, const int nchar_length, const DB_C_NCHAR str, const int nchar_str_byte_size, const int codeset, const int collation_id)
int db_log_dbval(DB_VALUE *result, DB_VALUE *value1, DB_VALUE *value2)
Definition: arithmetic.c:2603
int db_bit_count_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:4178
#define PT_ERRORmf2(parser, node, setNo, msgNo, arg1, arg2)
Definition: parse_tree.h:64
#define DB_INT32_MIN
Definition: dbtype_def.h:657
int db_make_monetary(DB_VALUE *value, const DB_CURRENCY type, const double amount)
int db_string_elt(DB_VALUE *result, DB_VALUE *arg[], int const num_args)
PT_MISC_TYPE qualifier
Definition: parse_tree.h:2219
#define PT_IS_INPUT_HOSTVAR(n)
Definition: parse_tree.h:340
#define PT_IS_QUERY_NODE_TYPE(x)
Definition: parse_tree.h:114
#define LANG_COERCIBLE_CODESET
int db_json_valid_dbval(const DB_VALUE *json, DB_VALUE *type_res)
Definition: arithmetic.c:5149
void * arg
Definition: expat.h:582
union pt_arg_type_val PT_ARG_TYPE_VAL
#define OR_CHECK_INT_DIV_OVERFLOW(a, b)
int prm_get_integer_value(PARAM_ID prm_id)
#define ER_TIME_CONVERSION
Definition: error_code.h:977
#define ER_GENERIC_ERROR
Definition: error_code.h:45
bool pt_check_cast_op(PARSER_CONTEXT *parser, PT_NODE *node)
DB_OBJECT * op
Definition: parse_tree.h:3052
TP_DOMAIN * tp_infer_common_domain(TP_DOMAIN *arg1, TP_DOMAIN *arg2)
char * db_get_database_name(void)
Definition: db_admin.c:417
PT_TYPE_ENUM virt_type_enum
Definition: parse_tree.h:2056
int tp_domain_status_er_set(TP_DOMAIN_STATUS status, const char *file_name, const int line_no, const DB_VALUE *src, const TP_DOMAIN *domain)
int db_json_type_dbval(const DB_VALUE *json, DB_VALUE *type_res)
Definition: arithmetic.c:5130
DB_VALUE db_value
Definition: parse_tree.h:3073
struct expression_definition EXPRESSION_DEFINITION
EXPRESSION_SIGNATURE overloads[MAX_OVERLOADS]
TP_DOMAIN_COLL_ACTION
Definition: object_domain.h:64
int db_string_replace(const DB_VALUE *src_string, const DB_VALUE *srch_string, const DB_VALUE *repl_string, DB_VALUE *replaced_string)
char * db_get_user_and_host_name(void)
Definition: db_admin.c:1922
#define PT_SET_NODE_COLL_MODIFIER(p, coll)
Definition: parse_tree.h:630
#define OR_CHECK_UNS_ADD_OVERFLOW(a, b, c)
const char * pt_show_type_enum(PT_TYPE_ENUM t)
PT_NODE * pt_wrap_collection_with_cast_op(PARSER_CONTEXT *parser, PT_NODE *arg, PT_TYPE_ENUM set_type, PT_NODE *set_data, bool for_collation)
PT_NODE * cte_pointer
Definition: parse_tree.h:2148
#define ER_IT_DATA_OVERFLOW
Definition: error_code.h:501
#define PT_IS_BIT_STRING_TYPE(t)
Definition: parse_tree.h:169
#define ER_OUT_OF_VIRTUAL_MEMORY
Definition: error_code.h:46
PT_NODE * on_cond
Definition: parse_tree.h:2164
#define PT_GET_COLLATION_MODIFIER(p)
Definition: parse_tree.h:623
#define PT_DOES_FUNCTION_HAVE_DIFFERENT_ARGS(op)
Definition: parse_tree.h:436
#define DB_ENUM_OVERFLOW_VAL
Definition: dbtype_def.h:670
#define PT_ARE_COMPARABLE(typ1, typ2)
PT_NODE * entity
Definition: parse_tree.h:2052
LPOLESTR DBORDINAL flags dbtype
#define MSGCAT_SET_PARSER_RUNTIME
int db_format(const DB_VALUE *value, const DB_VALUE *decimals, const DB_VALUE *number_lang, DB_VALUE *result, const TP_DOMAIN *domain)
PT_DATA_VALUE data_value
Definition: parse_tree.h:3072
#define MORE_COERCIBLE(arg1_coll_infer, arg2_coll_infer)
#define MSGCAT_SEMANTIC_DATE_UNDERFLOW
int db_json_keys(DB_VALUE *result, DB_VALUE *arg[], int const num_args)
PT_NODE * top_node
Definition: parse_tree.h:1720
PT_NODE * pt_check_type_compatibility_of_values_query(PARSER_CONTEXT *parser, PT_NODE *node)
int db_time_encode(DB_TIME *timeval, int hour, int minute, int second)
Definition: db_date.c:370
PT_MISC_TYPE all_distinct
Definition: parse_tree.h:2761
int db_string_compare(const DB_VALUE *string1, const DB_VALUE *string2, DB_VALUE *result)
int db_json_pretty_dbval(DB_VALUE *json, DB_VALUE *res)
Definition: arithmetic.c:5228
#define MSGCAT_SEMANTIC_OVERFLOW_COERCING_TO
const char * original
Definition: parse_tree.h:2559
#define ER_ATTEMPT_TO_USE_ZERODATE
Definition: error_code.h:1474
DB_DATETIME datetime
Definition: dbtype_def.h:808
#define PT_ERRORmf4(parser, node, setNo, msgNo, arg1, arg2, arg3, arg4)
Definition: parse_tree.h:66
#define PT_IS_PARAMETERIZED_TYPE(t)
Definition: parse_tree.h:229
_Bigint * b1
Definition: mprec.c:178
int db_tan_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:3801
PT_HINT_ENUM hint
Definition: parse_tree.h:2722
int intl_identifier_casecmp(const char *str1, const char *str2)
int db_string_pad(const MISC_OPERAND pad_operand, const DB_VALUE *src_string, const DB_VALUE *pad_length, const DB_VALUE *pad_charset, DB_VALUE *padded_string)
#define DB_VALUE_DOMAIN_TYPE(value)
Definition: dbtype.h:68
int db_to_datetime(const DB_VALUE *src_str, const DB_VALUE *format_str, const DB_VALUE *date_lang, const DB_TYPE type, DB_VALUE *result_datetime)
#define DB_INT32_MAX
Definition: dbtype_def.h:658
int db_ascii(const DB_VALUE *param, DB_VALUE *result)
DB_DOMAIN * db_method_return_domain(DB_METHOD *method)
Definition: db_info.c:1647
PT_ARG_TYPE_VAL val
#define PT_IS_LEFT_RECURSIVE_EXPRESSION(node)
int db_string_substring_index(DB_VALUE *src_string, DB_VALUE *delim_string, const DB_VALUE *count, DB_VALUE *result)
struct compare_between_operator COMPARE_BETWEEN_OPERATOR
bool print_collation
Definition: parse_tree.h:3079
int db_get_date_item(const DB_VALUE *src_date, const int item_type, DB_VALUE *result)
#define TP_DOMAIN_COLLATION(dom)
PT_NODE_TYPE node_type
Definition: parse_tree.h:3427
DB_BIGINT bigint
Definition: parse_tree.h:3046
#define TP_IS_NUMERIC_TYPE(typeid)
int db_string_upper(const DB_VALUE *string, DB_VALUE *upper_string)
int db_json_length_dbval(const DB_VALUE *json, const DB_VALUE *path, DB_VALUE *res)
Definition: arithmetic.c:5164
#define PT_IS_NATIONAL_CHAR_STRING_TYPE(t)
Definition: parse_tree.h:155
const char * db_error_string(int level)
Definition: db_admin.c:2106
#define MSGCAT_SEMANTIC_NOT_SINGLE_COL
PT_NODE * parser_copy_tree(PARSER_CONTEXT *parser, const PT_NODE *tree)
#define MIN(a, b)
Definition: dict_private.h:69
#define PT_IS_VALUE_QUERY(n)
Definition: parse_tree.h:479
int db_acos_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:3872
#define OR_CHECK_INT_OVERFLOW(i)
#define TP_SMALLINT_AS_CHAR_LENGTH
#define NUM_F_INSERT_SUBSTRING_ARGS
int db_date_dbval(DB_VALUE *result, const DB_VALUE *date_value, const TP_DOMAIN *domain)
PT_NODE * having
Definition: parse_tree.h:2707
DB_DOMAIN * pt_data_type_to_db_domain(PARSER_CONTEXT *parser, PT_NODE *dt, const char *class_name)
Definition: parse_dbi.c:1805
struct expression_signature EXPRESSION_SIGNATURE
PT_NODE * from
Definition: parse_tree.h:2701
const char * pt_show_binopcode(PT_OP_TYPE n)
int db_make_timestamptz(DB_VALUE *value, const DB_C_TIMESTAMPTZ *ts_tz_val)
#define PT_IS_CONST(n)
Definition: parse_tree.h:359
unsigned is_alias_enabled_expr
Definition: parse_tree.h:3467
int db_string_put_cs_and_collation(DB_VALUE *value, const int codeset, const int collation_id)
Definition: db_macro.c:4158
PT_NODE * enumeration
Definition: parse_tree.h:2053
UINTPTR spec_id
Definition: parse_tree.h:2558
#define PT_IS_VALUE_NODE(n)
Definition: parse_tree.h:325
TP_DOMAIN_STATUS tp_value_cast(const DB_VALUE *src, DB_VALUE *dest, const TP_DOMAIN *desired_domain, bool implicit_coercion)
#define PT_IS_DISCRETE_NUMBER_TYPE(t)
Definition: parse_tree.h:134
#define TP_DOMAIN_TYPE(dom)
XML_EndCdataSectionHandler end
Definition: expat.h:541
PT_DATA_TYPE_INFO data_type
Definition: parse_tree.h:3274
#define TP_INTEGER_AS_CHAR_LENGTH
void db_update_row_count_cache(const int row_count)
Definition: db_admin.c:2914
#define PT_SET_VALUE_QUERY(n)
Definition: parse_tree.h:482
#define DB_DATE_MAX
Definition: dbtype_def.h:675
int db_timestamp(const DB_VALUE *src_datetime1, const DB_VALUE *src_time2, DB_VALUE *result_datetime)
SP_PARSER_CTX * parser
#define TP_BIGINT_AS_CHAR_LENGTH
PT_NODE * arg1
Definition: parse_tree.h:2212
PT_NODE * pt_find_entity(PARSER_CONTEXT *parser, const PT_NODE *scope, UINTPTR id)
int db_json_array_append(DB_VALUE *result, DB_VALUE *arg[], int const num_args)
int db_str_to_date(const DB_VALUE *str, const DB_VALUE *format, const DB_VALUE *date_lang, DB_VALUE *result, TP_DOMAIN *domain)
int db_string_translate(const DB_VALUE *src_string, const DB_VALUE *from_string, const DB_VALUE *to_string, DB_VALUE *transed_string)
#define NULL
Definition: freelistheap.h:19
PT_NODE * as_attr_list
Definition: parse_tree.h:2152
unsigned short db_get_enum_short(const DB_VALUE *value)
bool db_string_check_explicit_time(const char *str, int str_len)
Definition: db_date.c:1327
int db_blob_to_bit(const DB_VALUE *src_value, const DB_VALUE *length_value, DB_VALUE *result_value)
const char * er_msg(void)
struct pr_type * type
Definition: object_domain.h:78
int db_string_instr(const DB_VALUE *src_string, const DB_VALUE *sub_string, const DB_VALUE *start_pos, DB_VALUE *result)
switch(pDBProp->dwPropertyID)
Definition: Rowset.h:138
int db_date_add_interval_days(DB_VALUE *result, const DB_VALUE *date, const DB_VALUE *db_days)
#define LANG_COERCIBLE_COLL
unsigned has_outer_spec
Definition: parse_tree.h:2770
TP_DOMAIN * tp_domain_resolve_value(DB_VALUE *val, TP_DOMAIN *dbuf)
void tz_get_system_tz_region(TZ_REGION *tz_region)
Definition: tz_support.c:758
#define MAX(a, b)
Definition: dict_private.h:70
#define PT_IS_RIGHT_RECURSIVE_EXPRESSION(node)
int db_blob_from_file(const DB_VALUE *src_value, DB_VALUE *result_value)
#define MSGCAT_SEMANTIC_FUNC_NOT_DEFINED_ON
PT_NODE * pt_limit_to_numbering_expr(PARSER_CONTEXT *parser, PT_NODE *limit, PT_OP_TYPE num_op, bool is_gby_num)
int db_value_to_enumeration_value(const DB_VALUE *src, DB_VALUE *result, const TP_DOMAIN *enum_domain)
int db_get_time_item(const DB_VALUE *src_date, const int item_type, DB_VALUE *result)
#define TP_MONETARY_AS_CHAR_LENGTH
PT_NODE * pt_semantic_type(PARSER_CONTEXT *parser, PT_NODE *tree, SEMANTIC_CHK_INFO *sc_info_ptr)
#define MILLISECONDS_OF_ONE_DAY
TP_DOMAIN * tp_domain_cache(TP_DOMAIN *transient)
#define err(fd,...)
Definition: porting.h:425
#define PT_ERRORc(parser, node, msg)
Definition: parse_tree.h:54
#define TP_DATETIME_AS_CHAR_LENGTH
int csession_get_last_insert_id(DB_VALUE *value, bool update_last_insert_id)
int db_value_put_encoded_date(DB_VALUE *value, const DB_DATE *date)
Definition: db_macro.c:1377
int db_add_months(const DB_VALUE *src_date, const DB_VALUE *nmonth, DB_VALUE *result_date)
assert(k<=ptr->_max_k)
#define TP_BIGINT_PRECISION
int db_from_tz(DB_VALUE *time_val, DB_VALUE *tz, DB_VALUE *time_val_with_tz)
#define PT_IS_COLLECTION_TYPE(t)
Definition: parse_tree.h:142
int pt_coerce_value(PARSER_CONTEXT *parser, PT_NODE *src, PT_NODE *dest, PT_TYPE_ENUM desired_type, PT_NODE *data_type)
short db_value_is_in_workspace
Definition: parse_tree.h:3075
#define TP_DATETIMETZ_AS_CHAR_LENGTH
#define db_private_free(thrd, ptr)
Definition: memory_alloc.h:230
#define PT_SPEC_IS_CTE(spec_)
Definition: parse_tree.h:698
int db_to_number(const DB_VALUE *src_str, const DB_VALUE *format_str, const DB_VALUE *number_lang, DB_VALUE *result_num)
int db_json_depth_dbval(DB_VALUE *json, DB_VALUE *res)
Definition: arithmetic.c:5213
#define OR_CHECK_SUB_UNDERFLOW(a, b, c)
int pt_check_same_datatype(const PARSER_CONTEXT *parser, const PT_NODE *p, const PT_NODE *q)
PT_NODE * arg1
Definition: parse_tree.h:2100
PT_QUERY_INFO query
Definition: parse_tree.h:3312
int numeric_db_value_mul(const DB_VALUE *dbv1, const DB_VALUE *dbv2, DB_VALUE *answer)
short location
Definition: parse_tree.h:2256
datanode * next
Definition: queue.h:31
int db_string_aes_encrypt(DB_VALUE const *src, DB_VALUE const *key, DB_VALUE *result)
int tp_domain_add(TP_DOMAIN **dlist, TP_DOMAIN *domain)
#define SET_EXPECTED_DOMAIN(node, dom)
Definition: type_checking.c:60
need_clear_type need_clear
Definition: dbtype_def.h:1109
int db_string_rlike(const DB_VALUE *src_string, const DB_VALUE *pattern, const DB_VALUE *case_sensitive, cub_regex_t **comp_regex, char **comp_pattern, int *result)
int db_get_cs_coll_info(DB_VALUE *result, const DB_VALUE *val, const int mode)
int db_date_diff(const DB_VALUE *date_value1, const DB_VALUE *date_value2, DB_VALUE *result)
int db_set_size(DB_SET *set)
Definition: db_set.c:552
int db_trunc_dbval(DB_VALUE *result, DB_VALUE *value1, DB_VALUE *value2)
Definition: arithmetic.c:3311
#define PT_IS_CAST_MAYBE(node)
int pr_clear_value(DB_VALUE *value)
#define cmp
Definition: mprec.h:351
const char * text
Definition: parse_tree.h:3070
int pt_evaluate_function(PARSER_CONTEXT *parser, PT_NODE *func, DB_VALUE *dbval_res)
#define TP_TIME_AS_CHAR_LENGTH
DB_BIGINT db_get_bigint(const DB_VALUE *value)
#define MSGCAT_SEMANTIC_AGG_FUN_WANT_1_ARG
int db_get_date_from_days(const DB_VALUE *src, DB_VALUE *result)
int db_string_char_length(const DB_VALUE *string, DB_VALUE *char_count)
#define DB_DEFAULT_NUMERIC_PRECISION
Definition: dbtype_def.h:589
int db_make_time(DB_VALUE *value, const int hour, const int minute, const int second)
#define DB_DEFAULT_SCALE
Definition: dbtype_def.h:586
PT_NODE * set
Definition: parse_tree.h:3060
bool pt_is_same_enum_data_type(PT_NODE *dt1, PT_NODE *dt2)
Definition: parse_dbi.c:147
int db_make_datetimetz(DB_VALUE *value, const DB_DATETIMETZ *datetimetz)
struct db_domain_info::general_info general_info
int64_t DB_BIGINT
Definition: dbtype_def.h:776
#define d1
Definition: mprec.c:789
#define ER_DATE_CONVERSION
Definition: error_code.h:238
#define PT_IS_FALSE_WHERE_VALUE(node)
Definition: parse_tree.h:705
int db_clob_to_char(const DB_VALUE *src_value, const DB_VALUE *codeset_value, DB_VALUE *result_value)
DB_METHOD * db_get_method(DB_OBJECT *obj, const char *name)
Definition: db_info.c:1455
PT_NODE * parser_append_node(PT_NODE *node, PT_NODE *list)
char * pt_short_print(PARSER_CONTEXT *parser, const PT_NODE *node)
int db_last_day(const DB_VALUE *src_date, DB_VALUE *result_day)
TP_DOMAIN * pt_xasl_type_enum_to_domain(const PT_TYPE_ENUM type)
int db_inet_aton(DB_VALUE *result_numbered_ip, const DB_VALUE *string)
#define TP_IS_CHAR_TYPE(typeid)
#define OR_CHECK_UNS_SUB_UNDERFLOW(a, b, c)
TP_DOMAIN_COLL_ACTION collation_flag
Definition: object_domain.h:96
bool pt_get_collation_info(PT_NODE *node, PT_COLL_INFER *coll_infer)
error($message)
Definition: run-tests.php:2122
PT_NODE * parser_new_node(PARSER_CONTEXT *parser, PT_NODE_TYPE node_type)
#define MSGCAT_SEMANTIC_COERCE_UNSUPPORTED
int db_make_float(DB_VALUE *value, const DB_C_FLOAT num)
void parser_free_node(const PARSER_CONTEXT *parser, PT_NODE *node)
Definition: parse_tree.c:847
int db_date_add_interval_expr(DB_VALUE *result, const DB_VALUE *date, const DB_VALUE *expr, const int unit)
int db_string_to_base64(DB_VALUE const *src, DB_VALUE *result)
#define PT_IS_HOSTVAR(n)
Definition: parse_tree.h:337
#define DB_DEFAULT_PRECISION
Definition: dbtype_def.h:583
int sm_att_info(MOP classop, const char *name, int *idp, TP_DOMAIN **domainp, int *sharedp, int class_attr)
#define true
Definition: system.h:94
#define TP_TYPE_HAS_COLLATION(typeid)
int db_cos_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:3737
#define OR_CHECK_FLOAT_OVERFLOW(i)
short db_value_is_initialized
Definition: parse_tree.h:3074
return k
Definition: mprec.c:281
#define ARG_FILE_LINE
Definition: error_manager.h:45
bool pt_is_json_path(PT_TYPE_ENUM type)
Definition: parse_tree.c:1444
const char * lang_get_codeset_name(int codeset_id)
#define DB_BIGINT_MAX
Definition: dbtype_def.h:665
int pr_clone_value(const DB_VALUE *src, DB_VALUE *dest)
void parser_free_tree(PARSER_CONTEXT *parser, PT_NODE *tree)
enum collation_result COLLATION_RESULT
#define MSGCAT_SEMANTIC_FUNC_NOT_DEFINED_ON_INDEX
#define TP_INTEGER_PRECISION
#define PT_IS_RECURSIVE_EXPRESSION(node)
#define DB_INT16_MAX
Definition: dbtype_def.h:655
a
Definition: mprec.c:189
unsigned int DB_TIME
Definition: dbtype_def.h:779
#define ER_AU_SELECT_FAILURE
Definition: error_code.h:217
int db_sqrt_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:723
int db_convert_time_to_sec(const DB_VALUE *src_date, DB_VALUE *result)
PT_COLL_COERC_LEV coerc_level
Definition: parse_tree.h:3651
unsigned int DB_DATE
Definition: dbtype_def.h:796
#define PT_IS_PRIMITIVE_TYPE(t)
Definition: parse_tree.h:225
#define MSGCAT_SET_PARSER_SEMANTIC
void pt_try_remove_order_by(PARSER_CONTEXT *parser, PT_NODE *query)
#define DB_BIGINT_MIN
Definition: dbtype_def.h:666
DB_DATA_STATUS
int db_get_date_dayofyear(const DB_VALUE *src_date, DB_VALUE *result)
PT_NODE * pt_check_union_compatibility(PARSER_CONTEXT *parser, PT_NODE *node)
int db_hex(const DB_VALUE *param, DB_VALUE *result)
#define free_and_init(ptr)
Definition: memory_alloc.h:148
char * db_get_database_version(void)
Definition: db_admin.c:440
#define PT_IS_COMPLEX_TYPE(t)
Definition: parse_tree.h:173
#define strlen(s1)
Definition: intl_support.c:45
int pt_common_collation(PT_COLL_INFER *arg1_coll_infer, PT_COLL_INFER *arg2_coll_infer, PT_COLL_INFER *arg3_coll_infer, const int args_w_coll, bool op_has_3_args, int *common_coll, INTL_CODESET *common_cs)
int db_string_trim(const MISC_OPERAND tr_operand, const DB_VALUE *trim_charset, const DB_VALUE *src_string, DB_VALUE *trimmed_string)
int db_json_extract_dbval(const DB_VALUE *json, const DB_VALUE *path, DB_VALUE *json_res)
Definition: arithmetic.c:5245
bool print_charset
Definition: parse_tree.h:3078
int db_make_timestampltz(DB_VALUE *value, const DB_C_TIMESTAMP ts_val)
PT_NODE * pt_make_prim_data_type(PARSER_CONTEXT *parser, PT_TYPE_ENUM e)
char * prm_get_string_value(PARAM_ID prm_id)
#define PT_HAS_DATE_PART(t)
Definition: parse_tree.h:204
DB_DATE * db_get_date(const DB_VALUE *value)
int db_convert_to_time(const DB_VALUE *src_hour, const DB_VALUE *src_minute, const DB_VALUE *src_second, DB_VALUE *result)
PT_NODE * default_value
Definition: parse_tree.h:2568
DB_METHOD * db_get_class_method(DB_OBJECT *obj, const char *name)
Definition: db_info.c:1485
#define ER_REGEX_EXEC_ERROR
Definition: error_code.h:1359
PT_NODE * non_recursive_part
Definition: parse_tree.h:1991
const char * tz_get_system_timezone(void)
Definition: tz_support.c:749
#define ER_OBJ_INVALID_ARGUMENTS
Definition: error_code.h:271
PT_OP_TYPE
Definition: parse_tree.h:1317
PT_NODE * connect_by
Definition: parse_tree.h:2704
int db_to_time(const DB_VALUE *src_str, const DB_VALUE *format_str, const DB_VALUE *date_lang, const DB_TYPE type, DB_VALUE *result_time)
unsigned int date
Definition: dbtype_def.h:801
int db_get_row_count_cache(void)
Definition: db_admin.c:2904
#define PT_IS_FUNCTION(n)
Definition: parse_tree.h:313
char * DB_C_CHAR
Definition: dbtype_def.h:1179
enum intl_codeset INTL_CODESET
Definition: intl_support.h:192
int pt_is_comp_op(PT_OP_TYPE op)
bool pt_is_json_object_name(PT_TYPE_ENUM type)
Definition: parse_tree.c:1438
LPOLESTR DBORDINAL flags BYTE precision
int db_radians_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:4086
int lang_set_flag_from_lang(const char *lang_str, bool has_user_format, bool has_user_lang, int *flag)
bool prm_get_bool_value(PARAM_ID prm_id)
int db_months_between(const DB_VALUE *start_mon, const DB_VALUE *end_mon, DB_VALUE *result_mon)
#define QSTR_IS_ANY_CHAR_OR_BIT(s)
Definition: string_opfunc.h:48
struct nodeptr node
PT_NODE * start_with
Definition: parse_tree.h:2705
DB_TIMESTAMP * db_get_timestamp(const DB_VALUE *value)
int db_get_string_size(const DB_VALUE *value)
int db_get_variable(DB_VALUE *name, DB_VALUE *value)
Definition: db_admin.c:1062
DB_C_SHORT db_get_short(const DB_VALUE *value)
int pt_is_single_tuple(PARSER_CONTEXT *parser, PT_NODE *select_node)
DB_C_CHAR db_get_enum_string(const DB_VALUE *value)
PT_NODE * after_cb_filter
Definition: parse_tree.h:2706
void er_clear(void)
int db_to_date(const DB_VALUE *src_str, const DB_VALUE *format_str, const DB_VALUE *date_lang, DB_VALUE *result_date)
#define TP_IS_DATE_TYPE(typeid)
int set_intersection(DB_COLLECTION *collection1, DB_COLLECTION *collection2, DB_COLLECTION **result, DB_DOMAIN *domain)
Definition: set_object.c:3942
const char * tz_get_session_local_timezone(void)
Definition: tz_support.c:739
#define TP_FLOATING_PRECISION_VALUE
char * pt_append_string(const PARSER_CONTEXT *parser, char *old_string, const char *new_tail)
Definition: parse_tree.c:963
int db_make_char(DB_VALUE *value, const int char_length, const DB_C_CHAR str, const int char_str_byte_size, const int codeset, const int collation_id)
int db_time_diff(const DB_VALUE *val1, const DB_VALUE *val2, DB_VALUE *result)
int db_crc32_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:5020
unsigned do_not_fold
Definition: parse_tree.h:3460
int db_least_or_greatest(DB_VALUE *arg1, DB_VALUE *arg2, DB_VALUE *result, bool least)
Definition: arithmetic.c:5280
char * parser_print_tree(PARSER_CONTEXT *parser, const PT_NODE *node)
#define DB_VALUE_TYPE(value)
Definition: dbtype.h:70
#define DB_CURRENCY_DEFAULT
Definition: dbtype.h:47
int db_string_like(const DB_VALUE *src_string, const DB_VALUE *pattern, const DB_VALUE *esc_char, int *result)
bool pt_is_expr_wrapped_function(PARSER_CONTEXT *parser, const PT_NODE *node)
int i
Definition: dynamic_load.c:955
PT_VALUE_INFO value
Definition: parse_tree.h:3345
int pt_length_of_select_list(PT_NODE *list, int hidden_col)
int db_to_char(const DB_VALUE *src_value, const DB_VALUE *format_or_length, const DB_VALUE *lang_str, DB_VALUE *result_str, const TP_DOMAIN *domain)
int db_make_null(DB_VALUE *value)
PT_NODE * pt_fold_union(PARSER_CONTEXT *parser, PT_NODE *union_node, STATEMENT_SET_FOLD fold_as)
#define calloc(m, n)
Definition: leak_detector.h:4
DB_TYPE id
PT_NODE * list
Definition: parse_tree.h:2700
#define DB_IS_NULL(value)
Definition: dbtype.h:66
PT_OP_TYPE op
Definition: parse_tree.h:2215
struct tp_domain * next
Definition: object_domain.h:76
int db_date_sub_interval_days(DB_VALUE *result, const DB_VALUE *date, const DB_VALUE *db_days)
int db_string_sha_two(DB_VALUE const *src, DB_VALUE const *hash_len, DB_VALUE *result)
#define pt_is_set_type(n)
Definition: parse_tree.h:266
bool pt_false_search_condition(PARSER_CONTEXT *parser, const PT_NODE *node)
enum pt_generic_type_enum PT_GENERIC_TYPE_ENUM
int db_make_double(DB_VALUE *value, const DB_C_DOUBLE num)
int db_mod_dbval(DB_VALUE *result, DB_VALUE *value1, DB_VALUE *value2)
Definition: arithmetic.c:1920
int db_date_sub_interval_expr(DB_VALUE *result, const DB_VALUE *date, const DB_VALUE *expr, const int unit)
int db_json_insert(DB_VALUE *result, DB_VALUE *arg[], int const num_args)
int db_string_bit_length(const DB_VALUE *string, DB_VALUE *bit_count)
int db_get_schema_def_dbval(DB_VALUE *result, DB_VALUE *name_val)
Definition: db_info.c:2408
int db_clob_from_file(const DB_VALUE *src_value, DB_VALUE *result_value)
int db_floor_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:79
void db_date_decode(const DB_DATE *date, int *monthp, int *dayp, int *yearp)
Definition: db_date.c:338
DB_DATETIME * db_get_datetime(const DB_VALUE *value)
int db_char_to_clob(const DB_VALUE *src_value, DB_VALUE *result_value)
#define DB_MAX_VARNCHAR_PRECISION
Definition: dbtype_def.h:571
int pt_evaluate_db_value_expr(PARSER_CONTEXT *parser, PT_NODE *expr, PT_OP_TYPE op, DB_VALUE *arg1, DB_VALUE *arg2, DB_VALUE *arg3, DB_VALUE *result, TP_DOMAIN *domain, PT_NODE *o1, PT_NODE *o2, PT_NODE *o3, PT_MISC_TYPE qualifier)
#define MSGCAT_RUNTIME_IS_NOT_AUTHORIZED_ON
#define MSGCAT_SEMANTIC_OP_NOT_DEFINED_ON
PT_NODE * pt_append_query_select_list(PARSER_CONTEXT *parser, PT_NODE *query, PT_NODE *attrs)
unsigned is_value_query
Definition: parse_tree.h:3463
#define PT_INTERNAL_ERROR(parser, what)
Definition: parse_tree.h:111
#define TP_SMALLINT_PRECISION
int db_string_repeat(const DB_VALUE *src_string, const DB_VALUE *count, DB_VALUE *result)
int db_make_timestamp(DB_VALUE *value, const DB_C_TIMESTAMP timeval)
int db_make_int(DB_VALUE *value, const int num)
#define PT_IS_NUMERIC_TYPE(t)
Definition: parse_tree.h:124
int db_get_string_length(const DB_VALUE *value)
int db_conv(const DB_VALUE *num, const DB_VALUE *from_base, const DB_VALUE *to_base, DB_VALUE *result)
#define MSGCAT_SEMANTIC_ZERO_DIVIDE
int db_tz_offset(const DB_VALUE *src_str, DB_VALUE *result_str, DB_DATETIME *datetime)
int db_make_oid(DB_VALUE *value, const OID *oid)
const char * alias_print
Definition: parse_tree.h:3443
enum pt_node_type PT_NODE_TYPE
Definition: parse_tree.h:903
PT_NODE * pt_domain_to_data_type(PARSER_CONTEXT *parser, DB_DOMAIN *domain)
int db_atan_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:3970
int db_sys_timezone(DB_VALUE *result_timezone)
const char * resolved
Definition: parse_tree.h:2560
XML_ParsingStatus * status
Definition: expat.h:785
int numeric_db_value_add(const DB_VALUE *dbv1, const DB_VALUE *dbv2, DB_VALUE *answer)
DB_TIMESTAMP DB_UTIME
Definition: dbtype_def.h:786
#define LANG_SYS_CODESET
#define TP_DOMAIN_CODESET(dom)
#define PT_HAS_COLLATION(t)
Definition: parse_tree.h:243
int collation_id
Definition: object_domain.h:94
int db_typeof_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:4302
int db_clob_length(const DB_VALUE *src_value, DB_VALUE *result_value)
DB_TIME * db_get_time(const DB_VALUE *value)
int db_string_md5(DB_VALUE const *val, DB_VALUE *result)
int db_json_object(DB_VALUE *result, DB_VALUE *arg[], int const num_args)
int db_get_row_count(int *row_count)
Definition: db_admin.c:1036
#define INTL_CAN_COERCE_CS(cs_from, cs_to)
Definition: intl_support.h:97
const char * au_user_name(void)
PT_NODE * pt_check_union_type_compatibility_of_values_query(PARSER_CONTEXT *parser, PT_NODE *node)
PT_NODE * pt_dbval_to_value(PARSER_CONTEXT *parser, const DB_VALUE *val)
Definition: parse_dbi.c:574
#define MSGCAT_SEMANTIC_INCOMPATIBLE_OPDS
int db_abs_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:557
DB_VALUE * default_value
Definition: esql_cli.c:348
void db_time_decode(DB_TIME *timeval, int *hourp, int *minutep, int *secondp)
Definition: db_date.c:432
int db_conv_tz(DB_VALUE *time_val, DB_VALUE *result_time)
PT_TYPE_ENUM type
DB_VALUE_COMPARE_RESULT
Definition: dbtype_def.h:226
type
int column_number
Definition: parse_tree.h:3430
int pt_length_of_list(const PT_NODE *list)
if( $result)
#define MSGCAT_SEMANTIC_FUNC_NOT_DEFINED_ON_3
PT_NODE * entity_name
Definition: parse_tree.h:2146
int pt_coerce_value_for_default_value(PARSER_CONTEXT *parser, PT_NODE *src, PT_NODE *dest, PT_TYPE_ENUM desired_type, PT_NODE *data_type, DB_DEFAULT_EXPR_TYPE default_expr_type)
#define DB_INT16_MIN
Definition: dbtype_def.h:654
PT_SELECT_INFO select
Definition: parse_tree.h:2793
DB_VALUE * pt_value_to_db(PARSER_CONTEXT *parser, PT_NODE *value)
Definition: parse_dbi.c:1088
#define MSGCAT_SEMANTIC_OP_NOT_DEFINED_ON_3
#define pt_is_const(n)
Definition: parse_tree.h:270
#define TP_DATE_AS_CHAR_LENGTH
int db_string_extract_dbval(const MISC_OPERAND extr_operand, DB_VALUE *dbval_p, DB_VALUE *result_p, TP_DOMAIN *domain_p)
double amount
Definition: dbtype_def.h:856
int setobj_size(COL *col)
Definition: set_object.c:4939
PT_HOST_VAR_INFO host_var
Definition: parse_tree.h:3297
int db_ceil_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:246
#define OR_CHECK_MULT_OVERFLOW(a, b, c)
PT_NODE * parser_walk_tree(PARSER_CONTEXT *parser, PT_NODE *node, PT_NODE_WALK_FUNCTION pre_function, void *pre_argument, PT_NODE_WALK_FUNCTION post_function, void *post_argument)
TP_DOMAIN_COLL_ACTION collation_flag
Definition: parse_tree.h:2062
#define PT_EXPR_INFO_IS_FLAGED(e, f)
Definition: parse_tree.h:2251
#define PT_ERRORf(parser, node, msg, arg1)
Definition: parse_tree.h:56
#define PT_IS_NAME_NODE(n)
Definition: parse_tree.h:316
int db_get_string_codeset(const DB_VALUE *value)
#define ER_OBJ_INVALID_METHOD
Definition: error_code.h:275
#define MSGCAT_SEMANTIC_FUNCTION_NO_ARGS
int db_degrees_dbval(DB_VALUE *result, DB_VALUE *value)
Definition: arithmetic.c:4054
unsigned int UINTPTR
Definition: system.h:208
PT_NODE * parser_copy_tree_list(PARSER_CONTEXT *parser, PT_NODE *tree)
#define ER_OBJ_NOT_A_CLASS
Definition: error_code.h:284
bool pt_is_symmetric_op(const PT_OP_TYPE op)
const char ** p
Definition: dynamic_load.c:946
MISC_OPERAND
#define PT_ERRORmf(parser, node, setNo, msgNo, arg1)
Definition: parse_tree.h:63
unsigned is_hidden_column
Definition: parse_tree.h:3455
int db_bit_to_blob(const DB_VALUE *src_value, DB_VALUE *result_value)
unsigned int time
Definition: dbtype_def.h:802
DB_DEFAULT_EXPR_TYPE
Definition: dbtype_def.h:1203
int db_get_date_week(const DB_VALUE *src_date, const DB_VALUE *mode, DB_VALUE *result)
PT_JOIN_TYPE join_type
Definition: parse_tree.h:2166
PT_COMP_TO_BETWEEN_OP_CODE_TYPE
Definition: parse_tree.h:1502
DB_C_BIT db_get_bit(const DB_VALUE *value, int *length)
#define PT_NAME_GENERATED_DERIVED_SPEC
Definition: parse_tree.h:2584
#define PT_EXPR_INFO_CAST_COLL_MODIFIER
Definition: parse_tree.h:2246
int db_like_bound(const DB_VALUE *const src_pattern, const DB_VALUE *const src_escape, DB_VALUE *const result_bound, const bool compute_lower_bound)
int db_value_domain_init(DB_VALUE *value, const DB_TYPE type, const int precision, const int scale)
Definition: db_macro.c:149
#define TP_DOMAIN_COLLATION_FLAG(dom)
PT_NODE * derived_table
Definition: parse_tree.h:2150
int pt_evaluate_function_w_args(PARSER_CONTEXT *parser, FUNC_TYPE fcode, DB_VALUE *args[], const int num_args, DB_VALUE *result)
int tz_datetimetz_fix_zone(const DB_DATETIMETZ *src_dt_tz, DB_DATETIMETZ *dest_dt_tz)
Definition: tz_support.c:1806
PT_NODE * range_var
Definition: parse_tree.h:2151
DB_C_CHAR db_get_string(const DB_VALUE *value)
#define DB_MAX_CHAR_PRECISION
Definition: dbtype_def.h:558
int db_width_bucket(DB_VALUE *result, const DB_VALUE *value1, const DB_VALUE *value2, const DB_VALUE *value3, const DB_VALUE *value4)
Definition: arithmetic.c:4598
const char int len
Definition: expat.h:713